Python代码在if语句之后停止运行



Python初学者。我正在尝试对一个程序进行故障排除;石头、纸、剪刀;游戏然而,代码在输出"0"之后停止运行;计算机的选择是"(x(";。错误是在函数中还是在模块中?

我也在使用自定义模块。

模块:

print('      Menu')
print('A.)   Rock')
print('B.)   Paper')
print('C.)   Scissors')

代码:

import random
def main():
"""The purpose of the main function is to take the user's input, validate it then compare it to the computer's generated answer to find the winner."""

gamesfunctions()
while True:
user_choice = input("Enter choice: ")
if user_choice.lower() not in ('a', 'b', 'c', 'd') and user_choice.upper() not in ('A', 'B', 'C', 'D'):
print("Not an appropriate choice.")
continue
else:
break

computer_choice = generate_computer_value()
check_winner(user_choice, computer_choice)

def generate_computer_value():
computer_value = random.randint(1,3)
return computer_value
def check_winner(user_choice, computer_value):
if computer_value == 1:
computer_value == "A" or computer_value == "a"
print("Computer choice is rock")
elif computer_value == 2:
computer_value == "B" or computer_value == "b"
print("Computer choice is paper")
elif computer_value == 3:
computer_value == "C" or computer_value == "c"
print("Computer choice is scissors")
elif user_choice == computer_value:
print('Same answer try again')
elif user_choice == "Rock" and computer_value == "C":
print('Rock smashes scissors, the game ends')
elif user_choice == "Scissors" and computer_value == "B":
print('Scissors cut paper, the game ends')
elif user_choice == "Paper" and computer_value == "A":
print('Paper wraps rock, the game ends')
elif computer_value == "A" and user_choice == "Scissors":
print('Rock smashes scissors, the game ends')
elif computer_value == "3" and user_choice == "Paper":
print('Scissors cut paper, the game ends')
else:
print('Paper wraps rock, the game ends')
main()

elif user_choice == computer_value: 

应该是if

但是,代码在输出"计算机的选择是"(x(";。是功能错误还是模块错误

这样做是因为您没有告诉您的代码要不断重复。你可以让它无限循环很容易。

while True:
main()

相关内容

最新更新