我是Python的初学者,正在为一个学校项目编写代码,但遇到了早期的错误
由于某种原因,我的if函数无法运行。
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
这是不正确的:
input()
if input == "Bronze":
input()
的工作方式是返回一个值。名称input
指的是函数本身,因此函数input
永远不会等于文本"Bronze"
,除非你明确地做了一些不好的事情,比如input = "Bronze"
(这很糟糕,因为如果你覆盖input
,你将无法再访问该函数(。
相反,您应该使用返回的值:
usr_input = input()
if usr_input == "Bronze":
此外,线路print(input("Back to return to menu screen"))
是不必要的复杂;print()
将打印input()
返回的任何内容,但input()
将显示"Back to return to menu screen"
提示而不将其包装在if
语句中。所以,input("Back to return to menu screen")
就是你所需要的。如果你保持原样,如果有人键入了一些文本,然后点击回车键,文本就会再次显示,因为print()
正在打印用户键入的任何文本。
您首先需要为输入分配一个变量,然后检查该变量是否等于"青铜;现在您正在接受输入,但没有将其存储在任何位置。所以固定的代码是
user_input = input()
if user_input == "Bronze":