我是python和一般编程人员的新手,我发现循环非常混乱。我不需要经常使用循环,但是我目前正在开发一个简单的游戏(脚,Nuke,蟑螂,类似于Rock,Paper和Scissors),而且我很确定我的基本逻辑已经成功,但是我无法获得如何在各回合中进行计数的逻辑,因为我必须将其绑定到if语句(我猜)或类似的东西。我还希望能够计算出玩家赢得了多少回合以及多少次是平局。玩家正在与一台根据随机数生成其回复的计算机对战。
import random
number = random.randint(1,3)
if number == 1:
chosen1 = "Foot"
elif number == 2:
chosen1 = "Nuke"
else:
chosen1 = "Cockroach"
chosen2 = input("Foot, Nuke or Cockroach? (Quit ends):")
def choice(chosen1, chosen2):
if (chosen1 == "Nuke" and chosen2=="Nuke"):
print("You chose: ", chosen2)
print("Computer chose: ", chosen1)
print("You LOSE!")
elif chosen1 == chosen2:
print("You chose: ", chosen2)
print("Computer chose: ", chosen1)
print("It is a tie!")
elif (chosen1 == "Foot" and chosen2=="Cockroach"):
print("You chose: ", chosen2)
print("Computer chose: ", chosen1)
print("You LOSE!")
elif (chosen2 == "Foot" and chosen1=="Cockroach"):
print("You chose: ", chosen2)
print("Computer chose: ", chosen1)
print("You WIN!")
elif (chosen1 == "Nuke" and chosen2=="Foot"):
print("You chose: ", chosen2)
print("Computer chose: ", chosen1)
print("You LOSE!")
elif (chosen2 == "Nuke" and chosen1=="Foot"):
print("You chose: ", chosen2)
print("Computer chose: ", chosen1)
print("You WIN!")
elif (chosen1 == "Cockroach" and chosen2=="Nuke"):
print("You chose: ", chosen2)
print("Computer chose: ", chosen1)
print("You LOSE!")
elif (chosen2 == "Cockroach" and chosen1=="Nuke"):
print("You chose: ", chosen2)
print("Computer chose: ", chosen1)
print("You WIN!")
elif chosen2 =="Quit":
quit()
while chosen2 != "Quit":
choice(chosen1, chosen2)
chosen2 = input("Foot, Nuke or Cockroach? (Quit ends):")
有人可以建议我如何处理此循环吗? 编辑:我希望在用户退出后打印获胜,平局和回合的次数。
答案 0 :(得分:0)
好吧,您可以在每个if语句中添加一个return语句。如果是平局,则返回0;如果玩家获胜,则返回1;否则,返回2。
然后只需添加
>> 9007199254740992 == 9007199254740993 ans = logical 1
这就是结果。然后将其与更多if进行比较,并添加平局和获胜变量。 这很丑,但应该可以。
答案 1 :(得分:0)
您的循环似乎没问题。您只需要将结果保存在可以更新的变量中即可。您可以在名为choice
的函数中为每个结果使用return语句,或者仅需一点点定义即可
wins = 0
ties = 0
在程序的开头,然后添加一个
wins += 1
# or ties += 1
程序的每个可能结果
答案 2 :(得分:0)
您可以像这样对赢和输进行查表:
who_wins = {('Cockroach','Nuke'): True,
('Foot', 'Cockroach'): True,
('Nuke', 'Foot'): True}
这样,您可以运行类似的功能
def check_winner(player, cpu):
result = who_wins.get((player,cpu), False)
return result
然后检查状态
result = check_winner(chosen2, chosen1)
if result:
print("You won!")
your_wins+=1
else:
print("You lost!")
computer_wins+=1
总共看起来像
import random
your_wins, cpu_wins = 0,0
who_wins = {('Cockroach','Nuke'): True,
('Foot', 'Cockroach'): True,
('Nuke', 'Foot'): True}
def check_winner(player, cpu):
result = who_wins.get((player,cpu), False)
return result
while True:
number = random.randint(1,3)
if number == 1:
chosen1 = "Foot"
elif number == 2:
chosen1 = "Nuke"
else:
chosen1 = "Cockroach"
chosen2 = input("Foot, Nuke or Cockroach? (Quit ends):")
if chosen2.lower() != "quit":
result = check_winner(chosen2, chosen1)
if result:
print("You won!")
your_wins += 1
else:
print("You lost!")
cpu_wins +=1
else:
quit()
答案 3 :(得分:0)
基本上:您需要声明一个计数变量,并在每次新回合开始时向该变量加1,然后可以打印该回合编号。如果您使用了数字(如您应该那样),则会得到一个错误,请使用str()方法来解决此问题。
我已经在repl.it上实现了 https://repl.it/@conradkay/WarpedPrevailingQuerylanguage
答案 4 :(得分:0)
import random
options = ['Foot','Nuke','Cockroach']
computer_number = random.randint(0,2)
computer_choice = options[computer_number]
def choice(chosen1, chosen2):
print("You chose: ", chosen2)
print("Computer chose: ", chosen1)
if (computer_choice,player_choice) == ('Nuke','Nuke'):
print("You LOSE!")
return 'Loss'
if (computer_choice==player_choice):
print("It is a tie!")
return 'Tie'
player_number = options.index(player_choice)
result = (player_number-computer_number)%3
if result == 2:
print(['You LOSE'])
return 'Loss'
if result == 1:
print(['You WIN!'])
return 'Win'
result_counts = {'Win':0,'Loss':0,'Tie':0}
while True:
player_choice = input("Foot, Nuke or Cockroach? (Quit ends):")
if player_choice == 'Quit':
break
result = choice(computer_choice, player_choice)
results_counts[result] = results_counts[result]+1
请注意,如果您希望计算机每次都做出不同的选择,则应将行
computer_number = random.randint(0,2)
computer_choice = options[computer_number]
在while
循环内。另外请注意,您忽略了很多有效性检查。