python - 在另一个输入()之后,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
工作?
解决方案
每个input
呼叫都等待键盘输入,因此当您呼叫input()
两次时,您要求输入两个键盘输入。
要修复,请将您的代码更改为如下内容:
user_input = input()
if user_input == 'D':
# go through the "dodge" scenario
elif user_input == 'A':
# go through the "attack" scenario
推荐阅读
- r - 使用 rvest 进行网页抓取:无法区分的 CSS 选择器阻止我抓取某些块
- html - 创建一个内部显示“是”“否”的开关切换
- laravel - 多态OneToMany图像-产品关系,如何知道哪些图像是哪些?
- ruby-on-rails - 生产中的rails 6 webpacker:rake assets:precompile失败
- javascript - Emscripten - 有没有办法从 JavaScript 中止执行?
- sql - CONTAINS 中的对象名称在数据库中未找到
- azure-active-directory - Okta - 需要将 SSO 重定向/中继到 Azure/Office 门户到 Web 应用程序
- css - React Material-UI 无法调整表格大小
- javascript - 鼠标 onclick 事件目标(移动侧导航)
- python-3.x - 高斯混合模型交叉验证