Elif命令即使输入响应也不起作用

时间:2018-07-05 18:41:21

标签: python

因此,我对python还是很陌生,我一直在尝试制作游戏。如下。

-询问姓名(完成)

-返回由每个名称的前两个字母组成的用户名(完成)

-要求用户玩(问题在这里)

发生的是,如果您选择“是”,它将掷骰子,但当您说“否”时,它是为了给您简单的再见,但是当您说“否”时,掷骰子仍然会掷骰子,然后返回结果并告诉您是否我赢了还是没赢。

其他一切似乎都可以正常工作,即使您输入对elif的响应,if仍然会在这里发生。如果有人可以帮我看看这个,我会很感激。

from random import randint

print ("Hello, whats is your first name?")
first_name = input()
print ("What is your second name?")
second_name = input()
username = first_name[0] + first_name[1] + second_name[0] + second_name[1]
print (username + ", I want to play a game. Do you accept?")
game = input()
if game.lower() == "yes":
print ("Great, let's play. I'm going to roll a dice, if it lands on a 6, you win. If not, you lose.")
dice_roll = (randint(1,6))
print (dice_roll)
if dice_roll == 6:
    print ("Congrats, you win.")
elif dice_roll != 6:
    print ("Sorry. You lose.")
elif game.lower() == "no":
    print ("Fine, leave then.")

2 个答案:

答案 0 :(得分:3)

我认为您的代码存在缩进问题。这就是即使用户输入的内容与"yes"

不同的原因,骰子仍会滚动的原因
from random import randint

print("Hello, whats is your first name?")
first_name = input()
print("What is your second name?")
second_name = input()
username = first_name[0] + first_name[1] + second_name[0] + second_name[1]
print (username + ", I want to play a game. Do you accept?")
game = input()
if game.lower() == "yes":
  print ("Great, let's play. I'm going to roll a dice, if it lands on a 6, you win. If not, you lose.")
  dice_roll = (randint(1,6))
  print (dice_roll)
  if dice_roll == 6:
    print ("Congrats, you win.")
  elif dice_roll != 6:
    print ("Sorry. You lose.")
elif game.lower() == "no":
    print ("Fine, leave then.")

答案 1 :(得分:2)

如果用户输入width: auto; ,则可以将掷骰子的逻辑包装在if语句中。否则,您可以跳转到yes语句并打印再见。

else