首页 > 解决方案 > 在另一个输入()之后,python中的输入()不起作用

问题描述

当我有一条带有 input() 的行时,当两者彼此之后,第一行之后的 input() 行将不起作用。这是我的代码。

from random import randint
you = 100
troll = 50
sword = randint(5,20)
goblin_attack = randint(25,50)
heal = you + 25
t_f = randint(1,2)
print("______________________")
print("WELCOME TO DRAGON GAME")
print("WRITE YOUR NAME......")
x = input()
print("Hello,")
print(x)
print("Lets begin....")
print("______________________")
print("You are in a troll cave")
print("A troll attacks!")
print("Quick, Dodge or attack,type D or A ")
print("______________________")
if input() == "D":
  print("You attempt to dodge out of it's swing!")
if t_f == 1:
  print("You dodge and attack!")
print("You hit the troll for")
print(sword)
print("damage")
print("It has")
print(troll - sword)
print("health left!")
if t_f == 2:
  print("You trip! you got hit for")
print(goblin_attack)
print("damage!")
print("You have")
print(you - goblin_attack)
you2 = you- goblin_attack
print("Health left!")
if input() == "A":
 print("You hit the troll for")
print(sword)
print("damage")
print("It has")
print(troll - sword)
print("health left!")

当我按 A 并回车时,什么都没有发生。但是当我按 D 时,它起作用了。无论我将输入更改为什么,第一个始终有效。任何人都知道我怎样才能使两者都input工作?

标签: python

解决方案


每个input呼叫都等待键盘输入,因此当您呼叫input()两次时,您要求输入两个键盘输入。

要修复,请将您的代码更改为如下内容:

user_input = input()
if user_input == 'D':
    # go through the "dodge" scenario
elif user_input == 'A':
    # go through the "attack" scenario

推荐阅读