简体   繁体   English

还是python的新手。 现在很困惑。 如何使该程序正常工作?

[英]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.

相关问题 我正在学习使用 python 制作机器人。 我该怎么做才能使用命令创建新频道? - Im learning to make a bot using python. What do I do so that I can create a new channel using the commands? 如何使我的特殊直角三角形程序工作? - How do I make my special right triangle program work? 我现在可以用打开的 cv 检测到蓝色,如果程序检测到蓝色它会打印一些东西,我该如何做到这一点? - I can now detect the colour blue with open cv, How do I make it so now if the program detects blue it prints something? 我使用 APScheduler,许多工作需要立即开始。 我配置了 next_run_time 但它不起作用? - I use APScheduler, many jobs need start right now. I configured next_run_time but it didn't work? 编程新手,学习python。 试图让这个程序工作 - New to programming, learning python. Trying to get this program to work 如何使用python生成器中现在可用的消息? - How do I consume messages that are available right now in a python generator? Python。 我如何捕捉<cntl> C 在程序的任何地方,然后清理?</cntl> - Python. How do I capture <cntl>C anywhere in a program, then clean up? 如何在python中使用mysql数据库调试cgi脚本。 我是如何调试的新手 - How do i debug my cgi script with mysql database in python. I am new to how to debugg 我如何使该程序在PyCharm中工作 - How do I make this program work in PyCharm Python - 如何使我的其他程序仅在 if 语句一致时执行? - Python - How do I make it so that my other program only executes when an if statement is in agreement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM