2017-03-07 41 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)") 
相关问题