我似乎无法将fprice从与投入相同而与增值税不同的方式进行更改
while True:
VAT=1
shoep = int(input("what is the price of the shoe (in euros)"))
country = input("what country are you buying the shoe from ")
if country == "Germany":
VAT=int(1.19)
elif country =="France":
VAT=int(1.20)
elif country == "Spain":
VAT=int(1.21)
elif country == "Italy":
VAT=int(1.22)
elif country == "Portugal":
VAT=int(1.23)
elif country != "Germany" or "Portugal" or "Italy" or "Spain"or "France" :
print("invalid input please try again")
fprice=shoep*VAT
fprice=float(fprice)
float(shoep)
float(VAT)
print("the price of the shoe is €",fprice)
发布于 2021-10-14 11:07:15
这一点:
VAT=int(1.19)
将导致VAT
的值为1
,因为1
是底值。当你向int
进行强制转换时,它将占据你给它的任何东西的发言权。
>>> int(1.9)
1
>>> int(1.12)
1
>>> int(2.1)
2
乘以任何东西,根据identity属性,将是那个东西。因此,12
输入将导致12
输出。
我认为你想要:
VAT=1.19
你也有一些奇怪的逻辑,试图处理任何不在你的列表中的国家。乍一看,您可以简单地使用else
。但是,您不需要再次请求国家/地区输入,因此将输出一个无效的(乘以1,增值税的默认值,result)。这样的结构可能会更好地为您服务:
from typing import Dict, Optional
taxes = {
"Germany": 1.19,
"France": 1.20,
"Spain": 1.21,
"Italy": 1.22,
"Portugal": 1.23,
}
def get_vat(taxes: Dict[str, float], msg: Optional[str] = None) -> float:
if msg is not None:
print(msg)
country = input("What country are you buying the shoe from?")
# Use the `get` functions ability to define a default to ask until the users get it right.
return taxes.get(country, get_vat(taxes, "You've selected an invalid country. Try again!")
while True:
shoe_price = float(input("what is the price of the shoe (in euros)") # You could use an int, but you're dropping info.
vat = get_vat(taxes)
fprice: float = shoe_price * vat # This will naturally be a float, no need to cast.
print("the price of the shoe is €",fprice)
发布于 2021-10-14 11:08:09
似乎无法将fprice从与输入相同更改为
int(1.<<anything>>)
始终为1
,因此将输入乘以1,得到相同的值
解决方案:删除int()
函数的使用。
您还应该使用import decimal
, and use this,因为它更适合货币精度
无关,"invalid input“条件不正确。如果要检查多个值,请使用in
elif country not in ["Germany", "Portugal", "Italy", "Spain", "France"]:
print("invalid input please try again")
发布于 2021-10-14 11:10:15
需要注意的几件事:
首先,VAT = int(1.22)
将值1.22
转换为整数1
。因此,无论哪个国家,增值税都将是1。您可以只说VAT = 1.22
(例如)
第二,条件
elif country != "Germany" or "Portugal" or "Italy" or "Spain" or "France"
并不是在做你认为的事情。
elif country != "Germany" or country != "Portugal" or country != "Italy" or ...
才是正确的方法。另一种方式:
elif country not in ["Germany", "Portugal", "Italy", ...]:
print("invalid input please try agaim")
https://stackoverflow.com/questions/69576077
复制相似问题