Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Help with variable glitch
-
Help wth None glitch. Whenever I click a button (rock, paper or scissors) it's meant to change the self.player; self.ruling and self.computer variables. Please help fix this.
Thanks in advance!
Here's the code:
# coding: utf-8 from scene import * import random choices = '🌚', '📄', '✂️' class rps(Scene): def __init__(self): self.player = None self.ruling = None self.computer = None def setup(self): self.button = Button(Rect(self.size.w/2-60, self.size.h/1-150, 125, 125)) self.button.background = Color(0,0,0,0) self.button.stroke = Color(0,0,0,0) self.button.image = 'Moon_2' self.button.action = self.rock_action self.add_layer(self.button) self.button1 = Button(Rect(self.size.w/2-60, self.size.h/1-290, 125, 125)) self.button1.background = Color(0,0,0,0) self.button1.stroke = Color(0,0,0,0) self.button1.image = 'Page_Facing_Up' self.button1.action = self.paper_action self.add_layer(self.button1) self.button2 = Button(Rect(self.size.w/2-60, self.size.h/1-450, 125, 125)) self.button2.background = Color(0,0,0,0) self.button2.stroke = Color(0,0,0,0) self.button2.image = 'Scissors' self.button2.action = self.scissors_action self.add_layer(self.button2) def rock_action(self): self.button.background = Color(0,0,0,0) self.player = '🌚' self.computer = random.choice(choices) run(ending()) def paper_action(self): self.button1.background = Color(0,0,0,0) self.player = '📄' self.computer = random.choice(choices) run(ending()) def scissors_action(self): self.button2.background = Color(0,0,0,0) self.player = '✂️' self.computer = random.choice(choices) run(ending()) def get_ruling(player_choice, opponent_choice): if player_choice == opponent_choice: return "Draw" elif player_choice == '🌚': return 'Win' if opponent_choice == '✂️' else 'Lose' elif player_choice == '📄': return 'Win' if opponent_choice == '🌚' else 'Lose' else: # player_choice == '✂️' return 'Win' if opponent_choice == '📄' else 'Lose' def draw(self): background(0, 0.05, 0.2) self.root_layer.update(self.dt) self.root_layer.draw() if self.player == "🌚" and self.computer == "🌚": self.ruling = "Draw" if self.player == "✂️" and self.computer == "✂️": self.ruling = "Draw" if self.player == "📄" and self.computer == "📄": self.ruling = "Draw" if self.player == "🌚" and self.computer == "✂️": self.ruling = "Win" if self.player == "🌚" and self.computer == "📄": self.ruling = "Lose" if self.player == "📄" and self.computer == "🌚": self.ruling = "Win" if self.player == "📄" and self.computer == "✂️": self.ruling = "Lose" if self.player == "✂️" and self.computer == "🌚": self.ruling = "Lose" if self.player == "✂️" and self.computer == "📄": self.ruling = "Win" global pp global cc global rr pp = self.player cc = self.computer rr = self.ruling class ending(Scene): def setup(self): self.next = Button(Rect(self.size.w/2+77, self.size.h/1-480, 80, 40), 'Next') self.next.background = Color(1,1,1) self.next.stroke = Color(1,1,1) self.next.action = self.next_action self.add_layer(self.next) def next_action(self): self.next.background = Color(1,1,1) self.player = '🌚' self.computer = random.choice(choices) run(rps(), PORTRAIT) def draw(self): background(0,0.05,0.2) tint(1,1,1) text('\nYou {}!\n\n\n\n'.format(rr, pp, cc), x=self.size.w/3+50, y=self.size.h/5*3+20, font_size=60) text('\n\n\n\nYou chose:\n{}\n\nComputer chose:\n{}'.format(rr, pp, cc), x=self.size.w/3+29, y=self.size.h/5*3, font_size=35) self.next.draw() run(rps(), PORTRAIT)
-
@Pythonistapro777 - you have chosen a user name that implies you are not a beginner and yet you are asking questions in a way that implies you are in fact a beginner not only in Pythonista, but Python and programming in general. You need to know that there are many highly skilled programmers involved in this forum who give a great deal of their time to help people at all levels. This is a precious resource and should not be squandered. The way you are asking for help is inappropriate and wasting their very valuable time.
You have been asked at least twice now to at least show that you have tried to figure out what the problem is yourself before asking for help. In the current case your are obviously (to me) having trouble using global variables and don't understand it. Please at least take the time to go learn something about global variables in Python and write some simple test code to verify that you understand it before posting a request for help again.
@omz - for the record - I personally consider this user's recent postings to be bordering on SPAM in these forums and would ask them to be treated as such if it continues.
-
@wradcliffe Not to mention ignoring general tips to improve programming overall provided by @ccc.
See here for a Basic RPS game and try and get some tips.
-
Pass the two values into the
__init__()
method ofending()
...class ending(Scene): def __init__(self, player, computer): self.player = player self.computer = computer self.ruling = get_ruling(player, computer)
See: https://github.com/Pythonistapro777/Magic8Ball/pull/5/files
-
@Pythonistapro777 , hey. Please don't take the comments here the wrong way. I have no idea how old you are. I am guessing you are pretty young. But the comments here are right. You need to do more to help yourself. The guys here are very nice and helpful. But you have been this for granted (maybe not intentionally). I also ask a lot of questions. But I do my own investigation/research and try to find out first myself. It's an important part of the learning process. With the simple things, it's seems like I am better than you, but the complex stuff it looks like you are going to be great. Way better than I will ever dream to be.
It's very clear you have an aptitude for programming. Please listen to these seasoned talented programmers and try to solve some of your problems yourself by reading and testing. If you still can't find the answer, post here with some useful information about what you tried.
I just wanted to write this, because you seem like a good guy. I am a old guy, and I find forums intimidating sometimes. From what I read, no one is angry with you, just frustrated with you. There is a big difference. I will to talk with you again soon about the splash screen idea I had. Happy programming, and keep your head held high. -
Well said, @Phuket2 . @Pythonistapro777 - if you are young, I am also one of the younger people here at 13. My picture is when I was younger, though.
-
Lol, yea. I get what they mean. I don't really put what I've tried on here 'cause I didn't it would really help but I'll work on everything. Also, I'm 14 - so bear with me.
-
@Pythonistapro777 , of course will bear with you. You young guys are the future. Just understand the comments the guys have been giving you is to help you develop your skills, I am sure no malice. Believe it or not, all us old guys were young once :)
Just believe we are behind you, wanting you to improve your skills. -
Thanks you @Phuket2