Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm attempting to program a stone, paper, scissors test system game in Python. It works generally, however incidentally, when I enter my decision of rock, paper, or scissors for the information doled out to variable "guess," nothing is returned. Much obliged to you! Here is the program:

def main():

    print('This game is rock, paper, scissors')

    number=userguess()

    num=computernumber()

    result(number, num)

#computernumber function assigns number to rock, paper, scissors

def computernumber():

    num=random.randint(1,4)

    if num==1:

        print('Computer picks rock')

    elif num==2:

        print('Computer picks paper')

    elif num==3:

        print('Computer picks scissors')

    return num

#userguess assigns rock, paper, scissors to a number

def userguess():

    guess=input("Choose, 'rock','paper',or 'scissors:") #<- this input prompt

    if valid(guess):

        if guess== 'rock':

            number=1

        elif guess== 'paper':

            number=2

        elif guess== 'scissors':

            number=3

        return number

    else:

        print('Please choose a valid response')

        return userguess()

def valid(guess):

    if guess=='rock' or guess=='paper' or guess=='scissors':

        result= True

    else:

        result= False

    return result

def restart():

    again=input('Play again? (y/n)')

    if again=='y':

        main()

    elif again=='n':

        print('Goodbye')

    else:

        print('please enter y or n')

        restart()

#function to display result

def result(num,number):

   if number==num:

       print('Tie')

       restart()

   elif number==1 and num==2:

       print('you won!')

       restart()

   elif number==1 and num==3:

       print ('you lost!')

       restart()

   elif number==2 and num==1:

       print('you lost!')

       restart()

   elif number==2 and num==3:

       print('you won!')

       restart()

   elif number==3 and num==1:

       print('you won!')

       restart()

   elif number==3 and num==2:

       print('you lost!')

       restart() 

main()

1 Answer

0 votes
by (26.4k points)

I think, Here is the problem.

#userguess assigns rock, paper, scissors to a number

def userguess():

    guess=input("Choose, 'rock','paper',or 'scissors:") #<- this input prompt

    if valid(guess):

        if guess== 'rock':

            number=1

        elif guess== 'paper':

            number=2

        elif guess== 'scissors':

            number=3

        return number

    else:

        print('Please choose a valid response')

        # return userguess() 

        # You returned a function which is None

        userguess()

I think your problem has been resolved

Additionally, I need to propose a few alternate ways to abbreviate the code. For instance, the above part can be abbreviate as follows:

str2num = {'rock': 1, 'paper': 2, 'scissors': 3} # can be defined globally

def userguess():

    guess=input("Choose, 'rock','paper',or 'scissors:") #<- this input prompt

    if valid(guess):

        return str2num[guess]

    else:

        print('Please choose a valid response')

        userguess()

Are you interested to learn python in detail? Come and Join the python course.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...