问题:
程序似乎不接受输入的整数。不会增加赢/输/平局计数,并且不会在调试模式下显示计算机选择
程序的基本设计:编写一个程序,让用户与计算机玩石头、剪刀、布游戏。
该程序应按如下方式工作。
将显示一个菜单:
战绩:0胜0平0负
(调试显示计算机的选择
(新游戏
(辞职
如果用户输入“Q”或“q”,程序将结束。 “N”或“n”表示新游戏,“D”或“d”表示调试模式,其他任何内容都会导致显示错误消息。
- 当游戏开始时,会生成一个 1 到 3 范围内的随机数。如果数字为 1,则计算机选择了 rock。如果数字是2,则计算机选择了纸张。如果数字是3,那么计算机选择了剪刀。 (除非我们处于“D”ebug 模式,否则不要显示计算机的选择。)
- 用户在键盘上输入他或她的选择“1-石头”、“2-布”或“3-剪刀”。
- 显示计算机的选择。
- 根据以下规则选出获胜者:
• 如果一名玩家选择石头,另一名玩家选择剪刀,则石头获胜。
(石头把剪刀砸碎了。)
• 如果一名玩家选择剪刀,另一名玩家选择布,则剪刀获胜。(剪刀剪布。)
• 如果一名玩家选择纸而另一名玩家选择石头,则纸获胜。
(纸包裹石头。)
• 如果两名玩家做出相同的选择,则游戏为平局。
- 您的程序将保存胜利、失败和平局的总数。
- 重新显示菜单并重复游戏循环。
我的计划:
import random
def main():
continuing = "y"
win = 0
lose = 0
draw = 0
while continuing == "y":
print("Score:", win,"wins,", draw, "draws,", lose,"losses")
print("(D)ebug to show computer's choice")
print("(N)ew game")
print("(Q)uit")
choice = input(" ")
if choice == "n" or choice == "N":
win, draw, lose = playgame(win, draw, lose)
elif choice == "d" or choice == "D":
win, draw, lose = playgame2(win, draw, lose)
elif choice == "q" or choice == "Q":
break
def playgame(win, draw, lose):
computer = random.randint(1,3)
player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")
if computer == 1 and player == 2:
Score = "You won"
win += 1
elif computer == 1 and player == 3:
Score = "You lost"
lose += 1
elif computer == 2 and player == 1:
Score = "You lost"
lose += 1
elif computer == 2 and player == 3:
Score = "You won"
win += 1
elif computer == 3 and player == 1:
Score = "You won"
win += 1
elif computer == 3 and player == 2:
Score = "You lost"
lose += 1
elif computer == player:
Score = "Draw"
draw += 1
return (win, draw, lose)
def playgame2(win, draw, lose):
computer = random.randint(1, 3)
player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")
if computer == 1 and player == 2:
Score = "You won"
print("Computer chose rock")
win += 1
elif computer == 1 and player == 3:
Score = "You lost"
print("Computer chose rock")
lose += 1
elif computer == 2 and player == 1:
Score = "You lost"
print("Computer chose paper")
lose += 1
elif computer == 2 and player == 3:
Score = "You won"
print("Computer chose paper")
win += 1
elif computer == 3 and player == 1:
Score = "You won"
print("Computer chose scissors")
win += 1
elif computer == 3 and player == 2:
Score = "You lost"
print("Computer chose scissors")
lose += 1
elif computer == player:
Score = "Draw"
print("Computer chose the same as you")
draw += 1
return (win, draw, lose)
main()