If语句只显示else部分



我正在为石头、纸、剪刀游戏编写代码,但函数identify_winner中的if语句没有运行。唯一打印出来的是else语句,它打印出所有结果,而不仅仅是在平局时。我很确定这与变量有关,但我不知道它是什么。

import random
ROCK = 1
PAPER = 2
SCISSORS = 3
def main():
    user_choose(None)
    comp_choose(None)
    identify_winner()
def user_choose(weapon):
    weapon = int(input('Choose Your Weapon' + 'n (Rock = 1, Paper = 2' +
                       ' Scissors = 3): '))
    if weapon == 1:
        print('You have chosen Rock')
    elif weapon == 2:
        print('You have chosen Paper')
    elif weapon == 3:
        print('You have chosen Scissors')
def comp_choose(choice):
    if random.randint(1,3) == 1:
        choice = 'Rock'
    elif random.randint(1,3) == 2:
        choice = 'Paper'
    else:
        choice = 'Scissors'
    print('Your enemy has chosen',choice)
def identify_winner():
    user = 0
    comp = 0
    while user == comp:
        user_choose(user)
        comp_choose(comp)
        if (user == 1 and comp == 3) or (user ==2 and comp == 1) or (user == 3 and comp 
        == 2):
            print('Congratulations! You have defeated the foe!')
        elif (comp ==1 and user == 3) or (comp == 2 and user == 1) or (comp == 3 and 
        user == 2):
            print('Alas, you have been defeated! Better luck next time!')
        else:
            print('Oh no, a tie! choose again!')
    
main()

首先,直接调用main函数并不是一个好的做法,因为它是一个脚本。若你们打算创建一个并没有脚本的程序,那个么你们应该在里面确定主要功能的范围。

if __name__ == "__main__":
  main()

其次,您不需要在identifywinner内部调用user_choose和comp_choose,只需在主程序中返回这些值,并将它们作为参数提供给identifywiner函数即可。此外,你不应该在comp_choose((中生成两倍的随机数,因为在第二个elif中,你可以生成上一个数字,所以comp的选择很可能是剪刀。我给你一个可能的解决方案:

import random
ROCK = 1
PAPER = 2
SCISSORS = 3
def main():
    identify_winner(user_choose(), comp_choose())
def user_choose():
    weapon = int(input('Choose Your Weapon' + 'n (Rock = 1, Paper = 2' +
                       ' Scissors = 3): '))
    if weapon == 1:
        print('You have chosen Rock')
    elif weapon == 2:
        print('You have chosen Paper')
    elif weapon == 3:
        print('You have chosen Scissors')
    
    return weapon
def comp_choose():
    comp_weapon = random.randint(1,3)
    if comp_weapon == 1:
        choice = 'Rock'
    elif comp_weapon == 2:
        choice = 'Paper'
    else:
        choice = 'Scissors'
    print('Your enemy has chosen',choice)
    return comp_weapon
def identify_winner(user, comp):
    
    if (user == 1 and comp == 3) or (user ==2 and comp == 1) or (user == 3 and comp 
    == 2):
        print('Congratulations! You have defeated the foe!')
    elif (comp ==1 and user == 3) or (comp == 2 and user == 1) or (comp == 3 and 
    user == 2):
        print('Alas, you have been defeated! Better luck next time!')
    else:
        print('Oh no, a tie! choose again!')

if __name__ == "__main__":
  main()

我对您的代码做了一些小的更改。对解释部分进行了评论:

import random
# you never use those, so they are not needed here:
# ROCK = 1
# PAPER = 2
# SCISSORS = 3
def user_choose(): # no input argument needed since you define weapon in the next line anyway
    weapon = int(input('Choose Your Weapon' + 'n (Rock = 1, Paper = 2' +
                       ' Scissors = 3): '))
    
    # this part is asking for a number as long as user don't choose a valid number between 1 and 3.
    # You could do even more here, check for number or letter, check if number is 0 or negative
    while weapon>3: 
        weapon = int(input('No valid number. Please choose again: ' + 'n (Rock = 1, Paper = 2' +
                       ' Scissors = 3): '))
        
    if weapon == 1:
        print('You have chosen Rock')
    elif weapon == 2:
        print('You have chosen Paper')
    elif weapon == 3:
        print('You have chosen Scissors')
    return weapon # you need to return the variable weapon, otherwise it is only in the local scope and your main function doesn't have access to it
def comp_choose(): # same as in the other function, no input argument needed
    choice = random.randint(1,3) # in your code random.randint(1,3) executes twice and can have two different results. You want it once and then check on it
    if choice == 1:
        chose = 'Rock' # in the main() func you compare the numbers, but in your code user has numbers between 1 and 3 and comp has values with rock, paper, scissors.
    elif choice == 2:
        chose = 'Paper'
    else:
        choice = 3
        chose = 'Scissors'
    print('Your enemy has chosen',chose)
    return choice # same as in the other function with 'weapon'
def main(): # identy_winner() isn't needed. two functions for user and comp with the main to select winner is enough
    run = True
    while run: # doing it like this you can make the user choose if he wants to continue or not (later at the 'continue_playing' part)
        user = user_choose() # get access to the return value of the function
        comp = comp_choose() # get access to the return value of the function
        if (user == 1 and comp == 3) or (user ==2 and comp == 1) or (user == 3 and comp 
        == 2):
            print('Congratulations! You have defeated the foe!')
        elif (comp ==1 and user == 3) or (comp == 2 and user == 1) or (comp == 3 and 
        user == 2):
            print('Alas, you have been defeated! Better luck next time!')
        else:
            print('Oh no, a tie! choose again!')
        
        continue_playing = input('You want to play again? [y/n]: ')
        if continue_playing == 'n':
            run = False
main()

如果用户选择一个字母而不是数字,代码就会崩溃,如果用户选择0或更少的数字,代码的工作就会很糟糕。你可能想检查一下。。。。我把这件事留给你。

最新更新