尝试根据 Python 中的条件语句设置变量
Attempting to set variable based on conditional statement in Python
所以今天实际上是我学习编程语言的第一天,所以我对 Python 非常陌生。我正在密歇根大学参加在线 Python 信息学课程,我们的第一个真正任务是创建一个简单的总工资率计算器。
这很简单,所以我决定扩展该程序的功能,以计算净工资和计税。我遇到的问题是确定如何根据一系列条件语句动态地(如果这是正确的词)设置变量 "taxrate" 的值。
我在 Python 的网站和 Stack Overflow 中搜索还没有找到答案。我认为我对编程的有限理解可能限制了我正确解释所发现内容的能力。
只是寻求一点帮助:
代码:
#This program is intended to calculate the net pay of employees
#This first section includes a loop for mistakes and finds gross pay
while True:
hours = raw_input('How many hours do you work weekly?')
hours1 = float(hours)
rate = raw_input('What is your hourly rate of pay?')
rate1 = float(rate)
grosspay = hours1 * rate1
taxstatus = raw_input('Do you pay taxes?')
#This secdtion is establishing the tax bracket the user falls into
taxbracket = taxrate
if grosspay <= 1000:
taxrate = 0.90
if grosspay > range(1000,1500):
taxrate = 0.78
if grosspay >= 1501:
taxrate = 0.63
# This section is intended to calculate pay after taxes
grosspay = hours1 * rate1
if taxstatus == 'yes':
netpay = grosspay * taxrate
print'Your weekly pay after taxes is',netpay
if not taxstatus:
print grosspay
当我在 PyCharm 中 运行 时,它让我知道 "taxrate" 尚未定义。我最终希望程序根据用户 "grosspay" 设置 "taxrate" 。我正在尝试做的事情可能吗?我假设它是而且我只是不明白该怎么做。
非常感谢任何帮助,如果有人想知道循环是干什么的,我有一个用户错误检查器,我在这部分之后正在处理程序完成。
你的逻辑在if grosspay > range(1000, 1500)
中有点可疑。 "greater" 比一系列数字意味着什么?我的猜测是您输入的 grosspay
实际上在 [1000, 1500)
范围内,因此它在您的代码中遇到了这个逻辑错误并且无法将其分配给任何东西。
检查数字是否在范围内的常用方法是使用 in
运算符。
if some_num in range(1, 10):
print("some_num is 1, 2, 3, 4, 5, 6, 7, 8, or 9")
但是您会注意到 some_num
必须 包含在整数范围 [1, 9]
中才能触发。如果 some_num
是 7.5
,这将失败。在总工资的情况下,这难以置信。某人的薪水完全相等的可能性有多大?
相反,您可以做的是:
if grosspay <= 1000:
taxrate = 0.90
elif 1000 < grosspay <= 1500:
taxrate = 0.78
elif 1500 < grosspay:
taxrage = 0.63
使用 elif
而不是一系列 if
使代码稍微更有效,因为 if/elif/else
根据定义是一个互斥的块。换句话说:
a = 1
b = 2
if a == 1:
print("This gets done!")
if b == 2:
print("This gets done!")
if a == 1:
print("This gets done!")
elif b == 2:
print("BUT THIS DOESN'T!")
else:
print("(this doesn't either...)")
所以今天实际上是我学习编程语言的第一天,所以我对 Python 非常陌生。我正在密歇根大学参加在线 Python 信息学课程,我们的第一个真正任务是创建一个简单的总工资率计算器。
这很简单,所以我决定扩展该程序的功能,以计算净工资和计税。我遇到的问题是确定如何根据一系列条件语句动态地(如果这是正确的词)设置变量 "taxrate" 的值。
我在 Python 的网站和 Stack Overflow 中搜索还没有找到答案。我认为我对编程的有限理解可能限制了我正确解释所发现内容的能力。
只是寻求一点帮助:
代码:
#This program is intended to calculate the net pay of employees
#This first section includes a loop for mistakes and finds gross pay
while True:
hours = raw_input('How many hours do you work weekly?')
hours1 = float(hours)
rate = raw_input('What is your hourly rate of pay?')
rate1 = float(rate)
grosspay = hours1 * rate1
taxstatus = raw_input('Do you pay taxes?')
#This secdtion is establishing the tax bracket the user falls into
taxbracket = taxrate
if grosspay <= 1000:
taxrate = 0.90
if grosspay > range(1000,1500):
taxrate = 0.78
if grosspay >= 1501:
taxrate = 0.63
# This section is intended to calculate pay after taxes
grosspay = hours1 * rate1
if taxstatus == 'yes':
netpay = grosspay * taxrate
print'Your weekly pay after taxes is',netpay
if not taxstatus:
print grosspay
当我在 PyCharm 中 运行 时,它让我知道 "taxrate" 尚未定义。我最终希望程序根据用户 "grosspay" 设置 "taxrate" 。我正在尝试做的事情可能吗?我假设它是而且我只是不明白该怎么做。
非常感谢任何帮助,如果有人想知道循环是干什么的,我有一个用户错误检查器,我在这部分之后正在处理程序完成。
你的逻辑在if grosspay > range(1000, 1500)
中有点可疑。 "greater" 比一系列数字意味着什么?我的猜测是您输入的 grosspay
实际上在 [1000, 1500)
范围内,因此它在您的代码中遇到了这个逻辑错误并且无法将其分配给任何东西。
检查数字是否在范围内的常用方法是使用 in
运算符。
if some_num in range(1, 10):
print("some_num is 1, 2, 3, 4, 5, 6, 7, 8, or 9")
但是您会注意到 some_num
必须 包含在整数范围 [1, 9]
中才能触发。如果 some_num
是 7.5
,这将失败。在总工资的情况下,这难以置信。某人的薪水完全相等的可能性有多大?
相反,您可以做的是:
if grosspay <= 1000:
taxrate = 0.90
elif 1000 < grosspay <= 1500:
taxrate = 0.78
elif 1500 < grosspay:
taxrage = 0.63
使用 elif
而不是一系列 if
使代码稍微更有效,因为 if/elif/else
根据定义是一个互斥的块。换句话说:
a = 1
b = 2
if a == 1:
print("This gets done!")
if b == 2:
print("This gets done!")
if a == 1:
print("This gets done!")
elif b == 2:
print("BUT THIS DOESN'T!")
else:
print("(this doesn't either...)")