2017-03-07 39 views
-5

嘿,如果他們對「重試」的回答是肯定的,我需要幫助爲這部分代碼創建一個循環。我對Python很陌生,互聯網似乎沒有幫助到目前爲止。乾杯!想要爲代碼的這一部分創建一個循環

print("Lets try an example!") 
time.sleep(2) 
input("Lets convert 9/2 into a mixed fraction! (Press Enter)") 
input("First we divide 9 by 2. This will give us 4 as the whole number and there will be 1/2 left over! (Press Enter)") 
print("when we put these two numbers in the correct format we get 4 and 1/2") 
print("Press Enter to continue") 
print("-------------------------------------------------------------------------------------------------------------------------------------------------") 
input("Next you are going to complete this conversion! (Press Enter)") 
divide = input("If we have the fraction 19/6, first we must divide the numerator by the denominator. Which number are we dividing by? ") 
if divide == "6": 
    print("Correct! We divide 19 by 6. This gives us 3 as our whole number and 1/6 remainder left over!") 
    time.sleep(1) 
    Fraction_part = input("What is the fraction part of this mixed fraction?") 
    if Fraction_part == "1/6": 
    print("Correct! This means our mixed fraction will be 3 and 1/6") 
    else: 
    print("Incorrect! 1/6 is our answer because we have 1/6 remiander") 
else: 
    print("Incorrect! 6 is the correct answer as it is the denominator in this fraction") 
    time.sleep(1) 
    Fraction_part = input("What is the fraction part of this mixed fraction?") 
    if Fraction_part == "1/6": 
    print("Correct! This means our mixed fraction will be 3 and 1/6") 
    else: 
    print("Incorrect! 1/6 is our answer because we have 1/6 remiander") 
retry = input("Again? (yes/no)") 
+1

互聯網幫助如果你谷歌「蟒蛇循環」 –

回答

1

將所有主代碼移動到一個函數中,如下例所示。

def doSomeThing(): 
     print("Lets try an example!") 
     ..... 
     print("Incorrect! 1/6 is our answer because we have 1/6 remiander") 
doSomeThing() 
retry = input("Again? (yes/no)") 
while(retry == "yes"): 
    doSomeThing() 
    retry = input("Again? (yes/no)") 
相關問題