I read the guidelines on posting homework questions and I think this one is within the rules. A little background, currently taking my first computer program class, its an online class (big mistake), it's very hard to get feedback or assistance.

I have been ripping my hair out trying to figure out this program. I already completed the flowchart and the instructor said it was correct. Now I'm trying to write the program from the flowchart and have hit a few bumps.

First off, in my while loop its supposed to validate either a 1 , 2 , 3 was entered. The instructor wants the Boolean to be != 1 and !=2 and !=3. After reading the text 20 times I still don't understand why not 1 and not 2 and not 3 is the proper way to do this. He even sent an email saying he was getting alot of questions and this is usually the hardest part of the program.

I have been tinkering with the loop for the better part of 4 hours and have officially run out of ideas.

I'm sure there are additional problems I will encounter later on, but is there any way someone could offer a hint to walk me through this?

Heres the code....Thanks

import random
#define the main function
def main():
    
    play_again ='y'
    
    com_choice()
    
    play_choice()
    
    deter_winner(com_choice,play_choice)
    
   
    while play_again == 'y' or play_again =='Y':
        com_choice()
        play_choice()
        print 'The computer selects', com_choice, 'for its throw',
        deter_winner(com_choice, play_choice)        
        play_again = raw_input ('Play again?')        
        
        
#computer choice is randomly generated   
def com_choice():   
    cchoice=random.randrange(1,4)
    return com_choice
    
def play_choice():        
    play_choice = raw_input
    while play_choice != 1 and play_choice != 2 and play_choice != 3:
        print 'Invalid choice'
        play_choice = (input('Enter a valid choice:'))
        
    
#determine the winner         
def deter_winner(play_choice,com_choice):        
    if com_choice == 1 and play_choice == 2:
        print 'Paper covers rock, you win.'
    elif com_choice == 2 and play_choice == 3:
        print 'Scissors cuts paper, you win.'
    elif com_choice == 3 and play_choice == 1:
        print 'Rock smashes scissors, you win.'
    elif com_choice == 1 and play_choice == 3:
        print 'Rock smashes scissors, you lose'
    elif com_choice == 2 and play_choice == 1:
        print 'Paper covers rock, you lose' 
    elif com_choice == 3 and play_choice == 2:
        print 'Scissors cuts paper, you lose'
    elif com_choice ==1 and play_choice == 1:
        print ',its a draw'
    elif com_choice == 2 and play_choice == 2:
        print ',its a draw'
    elif com_choice == 3 and play_choice == 3:
        print ',its a draw'
    else:
        print''
            
main()

I don't know. I'd probably just check that the user's input was in a tuple containing the appropriate input values.

the boolean != is used in the program so that if a user inputs a value other than ,2 and 3 the error mesage willl be displayed

hi,

def play_choice():
      play_choice = raw_input
      while play_choice != 1 and play_choice != 2 and play_choice != 3:
               print 'Invalid choice'
               play_choice = (input('Enter a valid choice:'))

Basically what this loop does is to ask the user for an input value. If the input value is not 1 and it is not 2 and it is not 3, the program prints an error message and asks the user for input again...and so on. :)

Edited 7 Years Ago by masterofpuppets: n/a

You might want to make some slight changes to your main and play_choice functions. I mean:

def main():
    play_again ='y'
    
    while play_again == 'y' or play_again =='Y':
         computerCh = com_choice()
         playerCh = play_choice()
         print 'The computer selects', com_choice, 'for its throw',
         deter_winner( computerCh, playerCh )
         play_again = raw_input ( 'Play again?' )

def play_choice():
    play_ch = raw_input
    while play_ch != 1 and play_ch != 2 and play_ch != 3:
        print 'Invalid choice'
        play_ch = ( input( 'Enter a valid choice:' ) )
    return play_ch

Edited 7 Years Ago by masterofpuppets: n/a

There's a couple problems in your play_choice function it looks like:

def play_choice():
    play_ch = raw_input #You're setting "play_ch" to the actual raw_input function. To get the return value from the function it should be "play_ch = raw_input()"
    while play_ch != 1 and play_ch != 2 and play_ch != 3:
        print 'Invalid choice'
        play_ch = ( input( 'Enter a valid choice:' ) ) # Should be using "raw_input" function here.
    return play_ch

I still don't understand why not 1 and not 2 and not 3 is the proper way to do this

Alternatives would be:

while (play_choice < 1) or (play_choice > 3):

while play_choice not in [1, 2, 3]:

# Also, you can use .lower instead of 2 compares
while play_again.lower() == 'y'':

Edited 7 Years Ago by woooee: n/a

There's a couple problems in your play_choice function it looks like:

def play_choice():
    play_ch = raw_input #You're setting "play_ch" to the actual raw_input function. To get the return value from the function it should be "play_ch = raw_input()"
    while play_ch != 1 and play_ch != 2 and play_ch != 3:
        print 'Invalid choice'
        play_ch = ( input( 'Enter a valid choice:' ) ) # Should be using "raw_input" function here.
    return play_ch

Yeah, you're right. I forgot to put the parentheses there :) sorry

I'll try to explain the not stuff in detail...

The != operator means 'does not equal', therefore the while loop can be translated into plane English as follows:

player_ch = raw_input()
while player_ch != "1" and player_ch != "2" and player_ch != "3":
      print 'Invalid choice'
      player_ch = raw_input( 'Enter a valid choice:' )
   
print player_ch

means:

"get an input string value and store it in the variable 'player_ch'.
while the value in the variable 'player_ch' does not equal "1" and
the value in the variable 'player_ch' does not equal "2" and
.........................' does not equal "3", keep asking the user for input."

e.g.
player_ch = "4", "4" != "1" and "4" != "2" and "4" != "3"
4 'does not equal' 3 and 4 does not equal 2 and 4 does not equal 1, therefore all conditions are true so ask the user for a new input.

hope that makes it clearer :)

Edited 7 Years Ago by masterofpuppets: n/a

This question has already been answered. Start a new discussion instead.