简体   繁体   English

如何让我的代码运行 if 语句?

[英]How do I get my code to run the if statement?

I'm a beginner in Python and I'm writing a code for a school project and ran into an early bug.我是 Python 的初学者,我正在为一个学校项目编写代码并遇到了一个早期的错误。
For some reason my if function won't run.由于某种原因,我的 if function 将无法运行。

import time            #imports computer time to program(buit in function)
count= 0

print("                                           Gymship")  # center this
print("--------------------------------------")  # this should go across the whole screen
print("Input a level to view the description or InputSign up to begin signing up for a card")
print("--------------------------------------------------------------------------")
print("Bronze")
time.sleep(1)  # this wil pause the program for 1 second(for effect)
 print("Silver")
time.sleep(1)
print("Gold")
time.sleep(1)
print("Platinum")
time.sleep(2)
print("-----------------------------------------------")  # this should go across the whole screen
print("Sign up")
print(" ")
input()
if input == "Bronze":
    print("Bronze")
    print("--------------------------------------------")
    print("You acquire a bronze card when you use two or less gym services")
    print("2 Hours limit in  the gym")
    print("-------------------------------------")
    print(input("Back to return to menu screen"))



count = count + 1

This is not correct:这是不正确的:

input()
if input == "Bronze":

The way input() works is by returning a value. input()的工作方式是返回一个值。 The name input refers to the function itself, so the function input will never equal the text "Bronze" unless you explicitly do something bad, like input = "Bronze" (it's bad because if you overwrite input , you'll no longer be able to access that function).名称input指的是 function 本身,因此 function input永远不会等于文本"Bronze" ,除非你明确地做了一些坏事,比如input = "Bronze" (这很糟糕,因为如果你覆盖input ,你将不再能够访问该功能)。

Instead, you should be using the returned value:相反,您应该使用返回的值:

usr_input = input()
if usr_input == "Bronze":

Also, the line print(input("Back to return to menu screen")) is unnecessarily complicated;此外,行print(input("Back to return to menu screen"))是不必要的复杂; the print() will print whatever was returned by input() , but input() will display the "Back to return to menu screen" prompt without wrapping it in an if statement. print()将打印input()返回的任何内容,但input()将显示"Back to return to menu screen"提示,而不将其包装在if语句中。 So, input("Back to return to menu screen") is all you need.因此,您只需要input("Back to return to menu screen") If you keep it the way you have it, if someone typed some text and then hit enter, the text would display again, because the print() is printing whatever that text was that the user typed.如果保持原样,如果有人输入了一些文本然后按回车,文本将再次显示,因为print()正在打印用户输入的任何文本。

You first need to assign a variable to the input and then check if the variable is equal to "Bronze" Right now you are taking the input, but are not storing it anywhere.您首先需要为输入分配一个变量,然后检查该变量是否等于“Bronze” 现在您正在接受输入,但没有将其存储在任何地方。 So the fixed code would be所以固定的代码是

user_input = input()
if user_input == "Bronze":

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我运行代码时,我的所有复选框都被选中。我该如何停止? - When i run the code, all my checkboxes get selected.How do i make this stop? 如何获取eval语句以将数字作为浮点数运行 - How do I get a eval statement to run the numbers as floats 如何使用 try 语句优化我的代码? - How do I optimize my code with try statement? 在Zapier中,如何获取我的Python“运行代码”操作的输入以列表形式而不是联接字符串形式传递? - In Zapier, how do I get the inputs to my Python “Run Code” action to be passed in as lists and not joined strings? 根据我是按语句还是作为块运行代码语句,如何获得不同的结果? - How is it possible that I get different results depending on if I run the code statement by statement or as a block? 如何为我的 else 语句获得正确的缩进? - How do I get the proper indent for my else statement? 如何让我的程序在 if 语句后继续 - How do I get my program to continue after an if statement 如何在此pandas语句中获取变量? - How do I get my variables in this pandas statement? 如何从Java运行python 2.7代码? - How do I run a python 2.7 code from my Java? 我如何使我的代码在python中的for循环中运行 - How do I make my code run in a for loop in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM