2015-04-03 76 views
0

好的,我剛剛開始學習python,不知道還有什麼地方可以提出這個問題。我寫了這個計算器,它的工作原理。我如何在python中無限循環這個程序?

a,b=input("Enter two numbers (sperated by a comma): ") 
operator=raw_input("Do you want to add, subtract, multiply, or divide? ") 
if operator=="divide": 
    print "Your answer is", a/b 
if operator=="multiply": 
    print "Your answer is", a * b 
if operator=="add": 
    print "Your answer is", a + b 
if operator=="subtract": 
    print "Your answer is", a - b  
repeat=raw_input("Do you want to perform another calculation(yes or no)? ") 

現在,我想知道如何重新啓動這個程序,如果輸入是肯定的。我搜索了一個答案,但我無法弄清楚循環整個事情的命令。就像在while循環中一樣,你必須在條件之後提供一個命令,但是該命令是什麼?哈爾普。

+0

可以把真實的情況 – 2015-04-03 11:39:55

回答

0
Try this
You can use while True: with this the code written inside the for loop
will execute infinitely,
To stop execution you can use Ctrl + C
while True:
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
    if operator=="divide":
        print "Your answer is", a/b
    if operator=="multiply":
        print "Your answer is", a * b
    if operator=="add":
        print "Your answer is", a + b
    if operator=="subtract":
        print "Your answer is", a - b
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")     if repeat == "no":         break
+3

這只是繼續循環永遠即使用戶有回答「不」重複 – 2015-04-03 11:45:20

+0

嘗試所做的新更改,希望它能夠正常工作 – abhijeetmote 2015-04-03 11:52:25

2
while True: 
    a,b=input("Enter two numbers (sperated by a comma): ") 
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ") 
    if operator=="divide": 
     print "Your answer is", a/b 
    if operator=="multiply": 
     print "Your answer is", a * b 
    if operator=="add": 
     print "Your answer is", a + b 
    if operator=="subtract": 
     print "Your answer is", a - b  
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ") 
    if repeat == 'no': 
     break 

雖然真正的永續循環,它可以通過關鍵字突破(它打破了最近的循環(for或while))被打破。

當處理來自用戶的輸入以確定條件時,最好檢查來自用戶的輸入是以「y」(是)開始還是以「n」(否)開始,因爲用戶可能會輸入y的是他可能惹的資本,下面是實現此代碼:

while True: 
    a,b=input("Enter two numbers (sperated by a comma): ") 
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ") 
    if operator=="divide": 
     print "Your answer is", a/b 
    if operator=="multiply": 
     print "Your answer is", a * b 
    if operator=="add": 
     print "Your answer is", a + b 
    if operator=="subtract": 
     print "Your answer is", a - b  
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ") 
    if repeat.lower.startswith('n'): 
     break 

「重複」是一個字符串,它有一個方法(類同功能)被稱爲「低」,則此方法返回小寫字母表示,那麼你可以通過使用另一個叫做startswith的方法來檢查小寫表示(也是一個字符串)是否以字母「n」開頭。只要答案是「是」(不區分大小寫)

repeat = "yes" 
while repeat.lower().strip() == 'yes': 
    a,b=input("Enter two numbers (sperated by a comma): ") 
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ") 
    if operator=="divide": 
     print "Your answer is", a/b 
    if operator=="multiply": 
     print "Your answer is", a * b 
    if operator=="add": 
     print "Your answer is", a + b 
    if operator=="subtract": 
     print "Your answer is", a - b  
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ") 

這將循環:

3

把它包在while循環。最初的「重複」是保證循環至少運行一次。

順便說一句,你可以把你if考不上的字典:

import operator 
ops = {'divide': operator.div, 
     'multiply': operator.mul, 
     'add': operator.add, 
     'subtract': operator.sub} 
repeat = "yes" 
while repeat.lower().strip() == 'yes': 
    a,b=input("Enter two numbers (sperated by a comma): ") 
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ").lower().strip() 
    if operator not in ops: 
     print 'Invalid operator:', operator 
     continue 
    print "Your answer is", ops[operator](a,b) 
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ") 
+1

挑剔:如果repeat.lower()。strip()在'yes'中:'...' - 許多程序都支持使用just 「y」而不是「yes」 – Dunno 2015-04-03 11:55:24

+0

我以爲那個問題,但問題具體要求問「(是或否)」,所以這就是我設定的有效答案。 – TheBlackCat 2015-04-03 11:58:51

0

試試這個:

while True:  
    a,b=input("Enter two numbers (sperated by a comma): ") 
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ") 
    if operator=="divide": 
     print "Your answer is", a/b 
    if operator=="multiply": 
     print "Your answer is", a * b 
    if operator=="add": 
     print "Your answer is", a + b 
    if operator=="subtract": 
     print "Your answer is", a - b  
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ") 
    if repeat == "no": 
     break