Forum Archive

Help with count

Pythonistapro777

In the RPS game, I will be recording the number of times they win/draw before losing.

Here's the code:

# coding: utf-8
from scene import *
import random
import sys

choices = '๐ŸŒš', '๐Ÿ“„', 'โœ‚๏ธ'

def get_ruling(player_choice, opponent_choice):
    if player_choice == opponent_choice:
        return "Draw"
    elif player_choice == '๐ŸŒš':
        return 'You win' if opponent_choice == 'โœ‚๏ธ' else 'You lose'
    elif player_choice == '๐Ÿ“„':
        return 'You win' if opponent_choice == '๐ŸŒš' else 'You lose'
    else: # player_choice == 'โœ‚๏ธ'
        return 'You win' if opponent_choice == '๐Ÿ“„' else 'You lose'

class rps(Scene):
    def __init__(self):
        self.player = 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-300, 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(self.player, self.computer))

    def paper_action(self):
        self.button1.background = Color(0,0,0,0)
        self.player = '๐Ÿ“„'
        self.computer = random.choice(choices)
        run(ending(self.player, self.computer))

    def scissors_action(self):
        self.button2.background = Color(0,0,0,0)
        self.player = 'โœ‚๏ธ'
        self.computer = random.choice(choices)
        run(ending(self.player, self.computer))

    def draw(self):
        background(0, 0.05, 0.2)
        self.root_layer.update(self.dt)
        self.root_layer.draw()

class ending(Scene):
    def __init__(self, player, computer):
        self.player = player
        self.computer = computer
        self.ruling = get_ruling(player, computer)

    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)
        self.done = Button(Rect(self.size.w/2+77, self.size.h/1-480, 80, 40), 'Done')
        self.done.background = Color(1,1,1)
        self.done.stroke = Color(1,1,1)
        self.done.action = self.done_action
        global stre
        stre = 0
        global count
        count = 0
        global streak
        streak = 0

    def next_action(self):
        self.next.background = Color(1,1,1)
        run(rps(), PORTRAIT)

    def done_action(self):
        self.done.background = Color(1,1,1)
        run(lose())

    def draw(self):
        background(0,0.05,0.2)
        self.root_layer.update(self.dt)
        self.root_layer.draw()
        tint(0.80, 0.40, 1.00)
        msg = '{}!'.format(self.ruling)
        if str(msg) == 'You lose!':
            self.add_layer(self.done)
        global stre
        global count
        global streak
        if str(msg) == 'You win!' or str(msg) == 'Draw!':
            while count != 1:
                stre = stre + 1
                streak = streak + stre
                count = count + 1
        text(msg, x=self.size.w/3+50, y=self.size.h/5*3+60, font_size=70)
        if self.player == '๐ŸŒš':
            self.name = 'Rock'
        if self.player == '๐Ÿ“„':
            self.name = 'Paper'
        if self.player == 'โœ‚๏ธ':
            self.name = 'Scissors'
        tint(1.00, 1.00, 0.00)
        msg = '\n\n\n\nYou chose:\n\n\nComputer chose:\n'
        name = '{}'.format(self.name)
        text(msg, x=self.size.w/3+29, y=self.size.h/5*3, font_size=35)
        tint(1,1,1)
        text(name, x=self.size.w/3-10, y=self.size.h/5*3-70, font_size=50)
        msg = '\n\n      {}\n\n'.format(self.player)
        text(msg, x=self.size.w/3+50, y=self.size.h/5*3-10, font_size=110)
        if self.computer == '๐ŸŒš':
            self.name3 = 'Rock'
        if self.computer == '๐Ÿ“„':
            self.name3 = 'Paper'
        if self.computer == 'โœ‚๏ธ':
            self.name3 = 'Scissors'
        name3 = '{}'.format(self.name3)
        msg = '\n\n\n\n        {}'.format(self.computer)
        text(msg, x=self.size.w/3+50, y=self.size.h/5*3-20, font_size=80)
        text(name3, x=self.size.w/3-10, y=self.size.h/5*3-200, font_size=50)
        if self.ruling == 'You lose!':
            self.add_layer(self.done)
        tint(0,1,0)
        msg = 'Streak: {}'.format(streak)
        text(msg, x=self.size.w/3-15, y=self.size.h/5*3+135, font_size=35)

run(rps(), PORTRAIT)

I've tried everything:
Flipping if and while statements
Changing the variables to None
Etc.

However I need it I be like this code:

count = 0
storage = 0
while count != 5:
    c = raw_input('')
    storage = storage + int(c)
    count = count + 1
    print(storage)

Where it adds up everything.

Thank in advance!

Also, if I am not explicit enough, please let me know

JonB

ok, lets work through this together.

in your second example, can you explain why it works? in other words, walk us through the initial conditions (count is initially set to 0), and tell us what you think will happen in each loop of the while loop. what causes the while loop to end?

Pythonistapro777

OK, in the second syntax - count is set to 0 and storage is set to 0. Count is then made the loop so that unless it equals five it asks the user to input a number and is then added to the previous numbers added. I t also increases the count. Then when count equals 5 everything stops. @JonB

ccc

Nice! Here are three ways to write the same loop:

# Number 1
count = 0
while count < 5:
    c = raw_input('Give it to me: ')
    count = count + 1

# Number 2 uses +=
count = 0
while count < 5:
    c = raw_input('Give it to me: ')
    count += 1

# Number 3 is considered the most "Pythonic"
for count in xrange(5):
    c = raw_input('Give it to me: ')
Phuket2

@Pythonistapro777, I don't want to interfere here. As I have said before, Scene, is black magic to me. But from what I can see, you need to think differently here.
I will say want I think... I really hope it's correct.

Imagine you rename your ending class to DisplayResults. In your main class, you add vars to the init method like self.win, self.lose etc...
When there is a win, self.win +=1

You could update those vars in your main class, when you call your DisplayResults class, you can call it with the needed values eg. DisplayClass(result_this_time, self.win, self.draw, self.lose)

I am trying to be a little vague... Hope it helps

JonB

Ok, so, you identified the key parts of a finite loop: it has a set of initial conditions, something which changes inside the loop, and an ending condition which is reachable from your initial conditions.

what would happen in your example if you started count at 6?

Next, why don't you walk us through your draw() function as it relates to the loop you are working on. maybe start from when someone has just won for the first time.... what will the starting and ending conditions be for the variables involed in the loop? next, assume the player continues, and wins again. what will the starting and ending conditions be for all of the variables in your loop?

A great way to answer such a question by experiment, is to add print statements at key places in your code. for instance, if that bit of code is not working the way you expect, put

print 'before loop',  count, stre, streak

before the loop, and

print 'after loop', count, stre, streak

after. do these match what you expected? If not, why not?

JonB

@Phuket2 Lets teach the man to fish, not serve him the เธ›เธฅเธฒ! ๐ŸŽฃ

Phuket2

@JonB, เธ–เธนเธเธ•เน‰เธญเธ‡. ๐Ÿ˜

ccc

เธ›เน‰เธฒเธขเธ”เธณ !!

Phuket2

Ok I stop now. I am sure he does not need a thai lesson :)but that was a difficult thai word for me, I had to ask my thai friend sitting next to me. Basically unauthorised taxis , not literally but local meaning.