Python 输入后脚本停止?
脚本在第一次输入后停止。(Python) 我做错了什么?Python 输入后脚本停止?,python,Python,脚本在第一次输入后停止。(Python) 我做错了什么?int(1)(或int(100))毫无意义,因为1已经是int。您可以改为: import decimal print("Choose how you want to calculate tax\n") print("Type 1 to calculate the tax amount\n") print("Type 2 to add tax to an amount\n") print("Type 3 to calculate the
int(1)
(或int(100)
)毫无意义,因为1
已经是int
。您可以改为:
import decimal
print("Choose how you want to calculate tax\n")
print("Type 1 to calculate the tax amount\n")
print("Type 2 to add tax to an amount\n")
print("Type 3 to calculate the amount without tax\n")
choice = input("Type 1, 2 or 3: ")
if choice == int(1):
amount = input("Enter an amount (with tax included): ")
tax = input("Enter the tax percentage: ")
amount = float(amount) / (int(100) + float(tax)) * int(100)
print("€" , round(amount,2))
或者更为稳健:
if int(choice) == 1:
# ...
零钱。比较前将选项更改为
int
if choice == '1':
# will not raise an error on non-digit input
因为
input
返回一个str
并且将str
与int(1)
进行比较(顺便说一下,int
位是冗余的)总是False
,所以您需要如果choice='1':
或者将输入转换为int
,然后进行比较,即choice=int(input…)
然后如果选择==1:
谢谢大家:)
...
choice = int(input("Type 1, 2 or 3: "))
if choice == 1:
...