游戏中"odd or even"逻辑错误



有时,当我选择odd,结果是odd时,我会认为计算机赢了,但它错了。

此外,分数也不起作用,分数总是打印1

import random
options = ["odd", "even"]
user_wins = 0
comp_wins = 0
while True:
user_answer = input("Choose odd or even. ").lower()
if user_answer not in options:
print("Try again.")
quit()
user_answer2 = input("Choose a number larger than zero. ")
if user_answer2.isdigit():
user_answer2 = int(user_answer2)
if user_answer2 <= 0:
print("Type a number larger than zero. ")
quit()
else:
print("Type a number next time.")
quit()
comp_pick = random.randint(0, 10)
if user_answer == "odd":
print("Computer picked even and its number is", comp_pick)
else:
print("Computer picked odd and its number is", comp_pick)
total = user_answer2 + comp_pick
if total % 2 == 0:
result = "even"
else:
result = "odd"
if result == "even" and user_answer == "even":
print("The result is", total, "You won!")
user_wins =+ 1
else:
print("The result is", total, "Computer won!")
comp_wins =+ 1
print("Your score:", user_wins)
print("Computer score:", comp_wins)
  1. 如果您因为上一个if语句而选择odd,您将无法获胜:
if result == "even" and user_answer == "even":

只有当您选择even时,它才能让您获胜。你可以试着把它改成

if result == user_answer:
  1. comp_wins =+ 1comp_wins = +1相同,后者与comp_wins = 1相同。你的意思一定是:
comp_wins += 1

user_wins =+ 1也是如此。

最新更新