[英]If statement inside a function python
如果 per 中的语句仅执行 else 行,则上述方法均无效。 此外,如果再次将薪水定义为薪水(小时,工作类型),则它不起作用。 为什么?
hour=int(input("Enter your hours of work: "))
worktype=input("Enter your worktype: ")
def per(worktype):
if worktype =="A":
return(8)
elif worktype =="B":
return(10)
else:
return(15)
x=per(worktype)
def salary(hour, x):
print("Your total salary is:", hour * x)
salary(hour, x)
您的代码工作正常。
IPython session:
>>> hour=int(input("Enter your hours of work: "))
...: worktype=input("Enter your worktype: ")
...:
...: def per(worktype):
...: if worktype =="A":
...: return(8)
...: elif worktype =="B":
...: return(10)
...: else:
...: return(15)
...:
...:
...: x=per(worktype)
...: def salary(hour, x):
...: print("Your total salary is:", hour * x)
...:
...: salary(hour, x)
Enter your hours of work: 10
Enter your worktype: B
Your total salary is: 100
在这里可能会绊倒您的是您以小写形式输入工作类型。 您可以通过添加来允许这样做
worktype = worktype.upper()
作为per
的第一行。
另外,我会搬家
x = per(worktype)
里面的salary
function。 它与全局变量无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.