为什么我不能在python 2.7.14中只写米而不是“米”?

时间:2017-12-20 15:13:25

标签: python

我使用python 2.7.14 .....我在python andriod Qpython3版本中编写了这段代码....但是当我在python 2.7.14中使用它时,程序接受带有这些“”或“”的字符串。 .....在Qpython3中我只需要写米......这里我要写“米”..... 任何建议如何解决?

这是代码:

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()

1 个答案:

答案 0 :(得分:0)

您无法在Python 2.x中使用input()。您应该使用raw_input()来返回字符串而不是引用变量。

因此,您编辑的代码应如下所示:

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(raw_input("Enter a number:"    )) 
    print(" ") 
    x=raw_input("from:"    )  
    print(" ")  
    y=raw_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=raw_input()
     if s=="next":
         print(" ")
         converter()
     elif s=="quit":
         time.sleep(1)
     else:
         print("I Can't understand your command")
     converter()

一些改进建议:

1:更好地格式化代码。我已经为你做了。

2:此处:elif s=="quit":您可以写:import syssys.exit()退出程序