[英]Still new to python. So confused right now. How do I make this program work?
So I'm making a little game to teach myself how python works, and it's been okay up to this point, but now I'm completely stuck. 因此,我正在做一个小游戏,以自学python的工作原理,到目前为止还可以,但是现在我完全陷入了困境。 I'm sure the logic and formatting of the if and while statement are all wrong, so I'd be happy if people could point me in the right direction - again, REALLY new to programming in general. 我确定if和while语句的逻辑和格式都是错误的,因此,如果人们能向我指出正确的方向,我将很高兴-再次说,这对编程来说真的很新。 I'll paste the code, then explain what I'm trying to make it do: 我将粘贴代码,然后解释我正在尝试做的事情:
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
If you run the program for yourself, everything is okay until you reach the "5, 4, 3, 2, 1, Fight!" 如果您自己运行程序,则一切正常,直到达到“ 5、4、3、2、1,战斗!”。 part. 部分。 Then when you type "attack," it gives you "Enter a valid response, young grasshopper." 然后,当您键入“攻击”时,它会为您“输入有效的答案,年轻的蚱hopper”。 I understand why this is happening, because "attack" doesn't fall under "rules", "commands", or "start". 我知道为什么会这样,因为“攻击”不属于“规则”,“命令”或“开始”。 I just don't get how I'm supposed to format it so that after I enter "attack", it actually proceeds and outputs the damage done, etc. I apologize if this is hard to understand, but I think if you run it for yourself, you'll understand what I'm having problems with. 我只是不知道如何格式化它,所以在我输入“攻击”之后,它实际上会继续进行并输出所造成的损害,等等。如果这很难理解,我深表歉意,但是我认为如果您运行它对于您自己,您将了解我遇到的问题。 Honestly, something tells me I've messed up the if and while statements up entirely, but hey, I'm learning and having fun :P. 老实说,有些事情告诉我,我完全搞砸了if和while语句,但是,嘿,我正在学习并很开心:P。
Thanks for any help. 谢谢你的帮助。
You aren't asking for more input. 您并不需要更多的输入。 It's guaranteed that the userInput
will still be "start"
at that point. 可以确保那时userInput
仍然是"start"
。
Your code would benefit greatly from the use of functions . 您的代码将从使用功能中受益匪浅。 You could really organize the code better that way. 您确实可以更好地组织代码。
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()
There are a lot of ways to do these things, and you'll get better with time. 有很多方法可以做这些事情,随着时间的流逝,您会变得更好。 Mainly note that user_input
only gets updated when you do this: 主要注意,只有在执行以下操作时才会更新user_input
:
user_input = (input())
Which in your code was only done once. 您的代码中哪个仅执行一次。
The code you have in the "start" branch of the if conditional will only run once, and userInput
will still be "start" at that point. if条件的“开始”分支中的代码将只运行一次, userInput
仍为“开始”。 The solution, wrap it in a loop. 解决方案,将其包装成一个循环。 Maybe something like this: 也许是这样的:
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)
The while loop will loop until the opponent has been defeated or until the user is defeated, but at the moment, there's no damage done to the user. while循环将一直循环,直到击败对手或直到击败用户为止,但是此刻,对用户没有任何伤害。 I'll leave that part to you. 我会把那部分留给你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.