所以我正在制作一个小游戏来教自己python是如何工作的,到目前为止一直没问题,但现在我完全陷入了困境。我确定if和while语句的逻辑和格式都是错误的,所以如果人们可以指出我正确的方向,我会很高兴 - 再次,一般来说,真的是编程新手。我将粘贴代码,然后解释我正在尝试做的事情:
import random
import time
print("Welcome to the Dojo!")
print("You have three opponents; they are ready...")
print("Are you?")
print("*To view the rules, type 'rules' ")
print("*To view commands, type 'commands' ")
print("*To begin, type 'start' ")
while True:
userInput = (input())
# Rules
if userInput == "rules":
print("The rules in this Dojo are simple. Kill your opponent! Fight to the death! Show no mercy!")
print("Each opponent gets progressively more difficult, whether it be in terms of health or damage.")
print("To attack, type 'attack'")
print("May the better (luckier) warrior win")
# Commands - to be added
elif userInput == "commands":
print("Commands will be added soon!")
# Start
elif userInput == "start":
damage = random.randint(1, 50)
userHealth = int(100)
opponentHealth = int(100)
print("Your first opponent is Larry Schmidt. Don't sweat it, he'll be a piece of cake.")
time.sleep(3)
print("The battle will begin in")
time.sleep(1)
print("5")
time.sleep(1)
print("4")
time.sleep(1)
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
print("Fight!")
if userInput == "attack":
int(userHealth - damage)
print("You did %(damage) to Larry!")
# Invalid response
else:
print("Enter a valid response, young grasshopper.")
if userInput == "start" is True:
continue
如果你自己运行程序,一切都没问题,直到你达到“5,4,3,2,1,战斗!”部分。然后当你输入“攻击”时,它会给你“输入一个有效的响应,年轻的蚱蜢”。我理解为什么会这样,因为“攻击”不属于“规则”,“命令”或“开始”。我只是不知道我应该如何格式化它以便在我进入“攻击”之后,它实际上继续并输出完成的损坏等等。如果这很难理解我很抱歉,但我想如果你运行它为了你自己,你会明白我遇到的问题。老实说,有些东西告诉我,我完全搞砸了if和while语句,但是嘿,我正在学习和玩乐:P。
感谢您的帮助。
答案 0 :(得分:2)
您不需要更多输入。它确保userInput
在此时仍为"start"
。
使用functions会使您的代码受益匪浅。你可以用这种方式更好地组织代码。
def intro():
print("Welcome to the Dojo!")
print("You have three opponents; they are ready...")
print("Are you?")
print("*To view the rules, type 'rules' ")
print("*To view commands, type 'commands' ")
print("*To begin, type 'start' ")
def get_user_input():
user_input = (input())
while user_input not in ['rules', 'commands', 'start']:
print("valid commands are rules, commands and start")
user_input = (input())
return user_input
intro()
returned_user_input = get_user_input()
有很多方法可以做这些事情,随着时间的推移,你会变得更好。主要注意user_input
只有在您执行此操作时才会更新:
user_input = (input())
您的代码中只执行了一次。
答案 1 :(得分:2)
if条件的“开始”分支中的代码只运行一次,userInput
在此时仍然是“开始”。解决方案,将其包裹在一个循环中。也许是这样的:
elif userInput == "start":
userHealth = 100
opponentHealth = 100
print("Your first opponent is Larry Schmidt. Don't sweat it, he'll be a piece of cake.")
time.sleep(3)
print("The battle will begin in")
time.sleep(1)
print("5")
time.sleep(1)
print("4")
time.sleep(1)
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
print("Fight!")
while opponentHealth > 0 and userHealth > 0:
userInput = input()
if userInput == "attack":
damage = random.randint(1, 50)
opponentHealth = opponentHealth - damage
print("You did %d to Larry!" % damage)
while循环将循环直到对手被击败或直到用户被击败,但此刻,用户没有受到伤害。我会把那部分留给你。