我正在努力用Python进行用户输入。 我想做的是提示用户浮动,不能为<到0,并提示另一个浮动,该浮动必须在0到100之间。
我尝试过try和ValueError,然后尝试if语句,并且只有在用户两次都不输入错字的情况下,它才有效。
这是到目前为止我最高级的代码:
try:
price = float(input("enter price : "))
except ValueError:
print(
"please enter a valid price. if u need to, use decimal separator '.' e.g. 6.66")
price = float(input("enter price : "))
discount = float(input("discount value ? "))
except ValueError:
print(
"please enter a valid discount, without the % sign. e.g. 25")
discount = float(input("discount value? "))
if discount >= 100:
print(
"please enter a valid discount, without the % sign. e.g. 25")
discount = float(input("discount value? "))
if discount <= 0:
print(
"please enter a valid discount, without the % sign. e.g. 25")
discount = float(input("discount value? "))
newprice = round(price * (100 - discount) / 100, 2)
print("article new value : ", newprice)