2013-04-25 57 views
4

我是新來的蟒蛇,我正在寫一個程序,將毫米轉換爲英寸。基本上它是一個連續的循環,可以讓你繼續輸入數字並獲得正確的轉換後的測量結果。我想把一個IF語句,允許用戶輸入「結束」來結束程序,而不是轉換更多的度量單位。我將如何去做這項工作(Python代碼允許你退出一個書面程序,並可以在IF語句中使用)?如何使用用戶輸入來結束程序?

convert=float(25.4) 
while True: 
    print("*******MM*******") 
    MM=float(input()) 
    Results=float(MM/convert) 
    print("*****Inches*****") 
    print("%.3f" % Results) 
    print("%.4f" % Results) 
    print("%.5f" % Results) 
+2

您可以使用'break'語句來停止while循環。 – 2013-04-25 13:22:28

+0

你想要做一些像'if ... sys.exit(1)'? – Vor 2013-04-25 13:24:07

+3

您的代碼已經有效。當我輸入'end'時,程序結束。請注意,它會在終止前拋出一個'NameError',但它_does_結束:-) – Kevin 2013-04-25 13:25:58

回答

2

您可以使用break擺脫循環。

MM=input() 
if MM == "end": 
    break 
+0

-1,'MM'已經是一個浮點數 – Vyktor 2013-04-25 13:22:48

+0

@Vyktor好點,但很容易修復,不知道它是否值得-1 ... – Aprillion 2013-04-25 13:28:26

+0

如果我改變了代碼convert = float(25.4) while True: 打印(「******* MM *******」) MM = float(input()) 結果= float(MM/convert) print(「***** Inches ** ***「) print(」%。3f「%Results) print(」%。4f「%Results) print(」%。5f「%Results) – 2013-04-25 13:28:59

0
import os 
print ("Convert MM to Inches") 
convert=float(25.4) 
while True: 
    print("*******MM*******") 
    MM=input() 
    if MM.lower() in ('end', 'quit'): 
     os._exit(1) 
    MM = float(MM) 
    Results=float(MM/convert) 
    print("*****Inches*****") 
    print("%.3f" % Results) 
    print("%.4f" % Results) 
    print("%.5f" % Results) 

print ("Convert MM to Inches") 
convert=float(25.4) 
while True: 
    print("*******MM*******") 
    MM=input() 
    if MM.lower() in ('end', 'quit'): 
     break 
    MM = float(MM) 
    Results=float(MM/convert) 
    print("*****Inches*****") 
    print("%.3f" % Results) 
    print("%.4f" % Results) 
    print("%.5f" % Results) 
+0

是的,我正在嘗試做一些事情,如果MM ==「結束」,然後退出程序 – 2013-04-25 13:28:05

+0

是的,這是我的第一個例子嗎?我只是把它作爲一個額外的選項來寫出退出:) – Torxed 2013-04-25 13:31:18

+0

@ user2316617也,你做了'MM = float(input())'這使得不可能檢查'MM =='字符串''因爲'MM'不再一個字符串,它是一個數字。 – Torxed 2013-04-25 13:33:12

1

測試對於MM後,它是由用戶輸入可以工作。您使用關鍵字break突破循環。你的例子,在小增加後如下。

print ("Convert MM to Inches") 
convert=float(25.4) 
while True: 
    print("*******MM*******") 
    MM=input() 
    if MM == 'end': 
     break 
    Results=float(MM)/convert 
    print("*****Inches*****") 
    print("%.3f" % Results) 
    print("%.4f" % Results) 
    print("%.5f" % Results) 
2

要結束循環,您可以使用break statement。你也可以採取事實上的優勢ValueErrorraised如果不兌換值的指令:

print ("Convert MM to Inches") 
convert=float(25.4) 
while True: 
    print("*******MM*******") 
    MM=input() 
    try: 
     MM = float(MM) 
    except ValueError: 
     break 

    Results=float(MM/convert) 
    print("*****Inches*****") 
    print("%.3f" % Results) 
    print("%.4f" % Results) 
    print("%.5f" % Results) 

結果:

[[email protected] tmp]$ ./convert 
Convert MM to Inches 
*******MM******* 
exit 
[[email protected] tmp]$ 

或者,如果你想退出只對字符串exit和如果發生錯誤,請轉到下一個循環,好的方法是:

MM = input() 
if MM == 'exit': 
    break 

try: 
    MM = float(MM) 
except ValueError: 
    print('I\'m sorry {} isn\'t a valid value'.format(MM)) 
    continue # Next iteration 

或者你可以把它「linuxy」可以等到按下Ctrl+C (Keyboard interrupt)並優雅地處理它:

try: 
    # Whole program goes here 
except KeyboardInterrupt: 
    print('Bye bye') 

這將是這樣的(^C手段發送Ctrl+C):

[[email protected] tmp]$ ./convert 
Convert MM to Inches 
*******MM******* 
^CBye bye 
+0

用於異常處理。 – 2013-04-25 13:38:45

4

只需使用iter加上哨點:

print ("Convert MM to Inches") 
convert=float(25.4) 
for i in iter(input, 'end'): 
    print("*******MM*******") 
    try: 
     MM=float(i) 
    except ValueError: 
     print("can't convert {}".format(i)) 
    else: 
     Results=float(MM/convert) 
     print("*****Inches*****") 
     print("%.3f" % Results) 
     print("%.4f" % Results) 
     print("%.5f" % Results) 

這樣,iter調用input並將其返回值存儲在i中,直到i == 'end'

請注意,如果用戶輸入非數字值(如上例),則可能需要進行一些錯誤檢查。

+0

這個問題用py3x標記,py3x中沒有'raw_input'。 – 2013-04-25 13:36:58

+0

我認爲他在Python 3中這樣做。難道他們沒有在Python 3中將'raw_input()'重命名爲'input()'? – 2013-04-25 13:37:21

+0

就我個人而言,我認爲這樣做有點草率。例如,如果用戶輸入「End」或「end」或「end」,會怎麼樣? – 2013-04-26 19:58:30

4

要結束循環,可以使用break語句。這可以在if語句中使用,因爲break只查看循環,而不查看條件。所以:

if user_input == "end": 
    break 

注意我是如何使用user_input,不MM?這是因爲你的代碼現在有一個小問題:你在查看用戶輸入的內容之前調用了float()。這意味着如果他們鍵入「結束」,你會調用float(「end」)並得到一個異常。更改您的代碼是這樣的:

user_input = input() 
if user_input == "end": 
    break 
MM = float(user_input) 
# Do your calculations and print your results 

一個更大的改善可以使:如果你希望用戶能夠輸入「END」或「結束」或「結束」,你可以用較低的( )方法來轉換輸入到比較之前小寫:

user_input = input() 
if user_input.lower() == "end": 
    break 
MM = float(user_input) 
# Do your calculations and print your results 

使所有這些變化,你的程序將工作,你希望它的方式。

+1

他使用Python 3. Python 3中的'input'工作原理是Python 2中的'raw_input'。 – Cairnarvon 2013-04-25 13:39:42

+0

謝謝:我錯過了。修正了我的答案來反映這一點。 – rmunn 2013-04-25 13:44:40

+0

您還應該建議修剪輸入中的空格,以防用戶在「結束」之前或之後意外擊中空格鍵 – 2013-04-26 19:59:22