为什么我的代码输出错误,将英寸转换为厘米,反之亦然
Why is my code giving wrong output, translating inches into centimetres and vice versa
我的代码给出了不正确的输出,但似乎一切正常。
代码的目的是将英寸转换为厘米,反之亦然。
例如,输入 cm
然后 6
,输出是:
inch: 6, centimeter: 15.24
But it should be:
英寸:2.362,厘米:6
Code:
```py
def intocm():
ms = input('What is it? (inch-in or centimeter-cm): ')
am = int(input('How many of it: '))
intocm = 2.54
global inch
global cm
if ms == 'inch' or 'in':
cm = am * intocm
inch = am
elif ms == 'centimeter' or ms == 'cm':
cm = am
inch = cm / intocm
print(f'inch: {inch}, centimeter: {cm}')
intocm()
您在 if
语句中错过了等式测试,您应该使用 float
(而不是 int
)。喜欢,
def intocm():
ms=input("What is it? (inch-in or centimeter-cm): ")
am=float(input("How many of it: "))
intocm=2.54
global inch
global cm
if ms=="inch" or ms=="in":
cm=am*intocm
inch=am
elif ms=="centimeter" or ms=="cm":
cm=am
inch=cm/intocm
print(f'inch: {inch}, centimeter: {cm}')
哪个我运行
What is it? (inch-in or centimeter-cm): cm
How many of it: 2.54
inch: 1.0, centimeter: 2.54
(两次)
What is it? (inch-in or centimeter-cm): in
How many of it: 1
inch: 1.0, centimeter: 2.54
这似乎是正确的。
如果 ms==“inch”或“in”是错误的。它应该是 if ms=="inch" 或 ms=="in"。否则它将被视为 if ((ms=="inch") or ("in"))
问题出在你的 if if ms=="inch" or "in":
中的逻辑上
你可以用 in
写得更好:
def intocm():
ms=input("What is it? (inch-in or centimeter-cm): ")
am=int(input("How many of it: "))
intocm=2.54
if ms in ["inch" , "in"]:
cm=am*intocm
inch=am
elif ms in ["centimeter" , "cm"]:
cm=am
inch=cm/intocm
print(f'inch: {inch}, centimeter: {cm}')
intocm()
代码因第一个 if 条件而失败。应该和你的elif条件差不多。
在你的代码中是
如果 ms==“英寸”或“在”:
但应该是
如果 ms=="inch" 或 ms=="in":
我的代码给出了不正确的输出,但似乎一切正常。
代码的目的是将英寸转换为厘米,反之亦然。
例如,输入 cm
然后 6
,输出是:
inch: 6, centimeter: 15.24
But it should be:
英寸:2.362,厘米:6
Code:
```py
def intocm():
ms = input('What is it? (inch-in or centimeter-cm): ')
am = int(input('How many of it: '))
intocm = 2.54
global inch
global cm
if ms == 'inch' or 'in':
cm = am * intocm
inch = am
elif ms == 'centimeter' or ms == 'cm':
cm = am
inch = cm / intocm
print(f'inch: {inch}, centimeter: {cm}')
intocm()
您在 if
语句中错过了等式测试,您应该使用 float
(而不是 int
)。喜欢,
def intocm():
ms=input("What is it? (inch-in or centimeter-cm): ")
am=float(input("How many of it: "))
intocm=2.54
global inch
global cm
if ms=="inch" or ms=="in":
cm=am*intocm
inch=am
elif ms=="centimeter" or ms=="cm":
cm=am
inch=cm/intocm
print(f'inch: {inch}, centimeter: {cm}')
哪个我运行
What is it? (inch-in or centimeter-cm): cm
How many of it: 2.54
inch: 1.0, centimeter: 2.54
(两次)
What is it? (inch-in or centimeter-cm): in
How many of it: 1
inch: 1.0, centimeter: 2.54
这似乎是正确的。
如果 ms==“inch”或“in”是错误的。它应该是 if ms=="inch" 或 ms=="in"。否则它将被视为 if ((ms=="inch") or ("in"))
问题出在你的 if if ms=="inch" or "in":
你可以用 in
写得更好:
def intocm():
ms=input("What is it? (inch-in or centimeter-cm): ")
am=int(input("How many of it: "))
intocm=2.54
if ms in ["inch" , "in"]:
cm=am*intocm
inch=am
elif ms in ["centimeter" , "cm"]:
cm=am
inch=cm/intocm
print(f'inch: {inch}, centimeter: {cm}')
intocm()
代码因第一个 if 条件而失败。应该和你的elif条件差不多。
在你的代码中是
如果 ms==“英寸”或“在”:
但应该是 如果 ms=="inch" 或 ms=="in":