想为这部分代码创建一个循环

时间:2017-03-07 07:02:37

标签: python loops

嘿,如果他们对Retry的回答是肯定的,我需要帮助为这段代码创建一个循环。我是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 个答案:

答案 0 :(得分: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)")
相关问题