我使用python 2.7.14。。。。。我用python andriod Qpython3版本写了这个代码…但是当我在Python2.7.14中使用它时,程序接受带有“”或“”的字符串…在Qpython3中我只需要写meter…这里我必须写“meter”。。。。。 有什么解决办法吗
代码如下:
import time
print("This is a converter.Here, you have to spell the whole name of a unit otherwise it wont work")
print(' ')
def converter():
a=float(input("Enter a number:" ))
print(" ")
x=input("from:" )
print(" ")
y=input("to:" )
print(" ")
if x=="meter" and y=="kilometer" or x=="gram" and y=="kilogram":
print(a/1000)
elif x=="kilometer" and y=="meter" or x=="kilogram" and y=="gram":
print(a*1000)
#just keep adding more convertions starting with elifs inside the define function
#and keep the else statement at the end of all elifs...
#and that converter() function call it at the all end just once...
#infact dont do anything escept adding elif statements....good luck
#dont change that float it accepts both integer and float
else :
print("wrong")
print(" ")
print('To continue type "next",Thank you.')
print(" ")
print('To exit type "quit",Thank you.')
print(" ")
s=input()
if s=="next":
print(" ")
converter()
elif s=="quit":
time.sleep(1)
else:
print("I Can't understand your command")
converter()
在python2.x中不能使用
input()
。您应该使用raw_input()
,它返回一个字符串,而不是引用一个变量因此,您编辑的代码应该如下所示:
一些改进建议:
1.:最好格式化代码。我为你做的
2.:这里:
elif s=="quit":
您可以编写:import sys
和sys.exit()
退出程序相关问题 更多 >
编程相关推荐