我试图在代码到达某一点后创建一个循环。怎么样?

时间:2018-02-23 17:47:09

标签: python

如何创建一个循环,重新询问您想要的公式?它应该问你是否想再做一个公式。你怎么循环呢?

   print("""
Welcome to the Science Speed, Distance, and Time Formula Caculator!

[1] Speed
[2] Distance
[3] Time

Enter the number NOT the word.
""")
choice1 = int(input("Choose a Formula: "))
if choice1 == 1:  
  print("You have choosen the Speed Formula!")
  D = int(input("Enter the Distance(meters): "))
  T = int(input("Enter the Time(seconds): "))
  Equation = D/T
  print ("Your answer is ",Equation,"m/s.")
  print ("Restart the program to do another equation.(Click Run^^^) This caculator was made in 33 lines of code!")
elif choice1 == 2:  
  print("You have choosen the Distance Formula!")
  S = int(input("Enter the Speed(m/s): "))
  T = int(input("Enter the Time(seconds): "))
  Equation = S*T
  print("Your answer is ",Equation,"meters.")
  print ("Restart the program to do another equation.(Click Run^^^) This caculator was made in 33 lines of code!")
elif choice1 == 3:  
  print("You have choosen the Time Formula!")
  D = int(input("Enter the Distance(meters): "))
  S = int(input("Enter the Speed(m/s): "))
  Equation = D/S 
  print("Your answer is ",Equation,"seconds.")
  print ("Restart the program to do another equation.(Click Run^^^) This caculator was made in 33 lines of code!")
else: 
  print("Restart the program and choose one of the numbers above for the corresponding formula you would like to calculate. This calculator was made in 33 lines of code!")

3 个答案:

答案 0 :(得分:0)

看起来你需要一个while循环。

<强>实施例

# -*- coding: utf-8 -*-

print("""
Welcome to the Science Speed, Distance, and Time Formula Caculator!

[1] Speed
[2] Distance
[3] Time

Enter the number NOT the word.
""")
while True:
    choice1 = int(input("Choose a Formula: "))
    if choice1 == 1:
      print("You have choosen the Speed Formula!")
      D = int(input("Enter the Distance(meters): "))
      T = int(input("Enter the Time(seconds): "))
      Equation = D/T
      print ("Your answer is ",Equation,"m/s.")
      print ("Restart the program to do another equation.(Click Run^^^) This caculator was made in 33 lines of code!")
    elif choice1 == 2:
      print("You have choosen the Distance Formula!")
      S = int(input("Enter the Speed(m/s): "))
      T = int(input("Enter the Time(seconds): "))
      Equation = S*T
      print("Your answer is ",Equation,"meters.")
      print ("Restart the program to do another equation.(Click Run^^^) This caculator was made in 33 lines of code!")
    elif choice1 == 3:
      print("You have choosen the Time Formula!")
      D = int(input("Enter the Distance(meters): "))
      S = int(input("Enter the Speed(m/s): "))
      Equation = D/S
      print("Your answer is ",Equation,"seconds.")
      print ("Restart the program to do another equation.(Click Run^^^) This caculator was made in 33 lines of code!")
    else:
      print("Restart the program and choose one of the numbers above for the corresponding formula you would like to calculate. This calculator was made in 33 lines of code!")

答案 1 :(得分:0)

String output = "line 10 bla bla bla line 20, line 30"

for(int i=1;i<=3;i++) {
    Scanner scanner = new Scanner(output);
    boolean found= false;
            while(scanner.hasNext() && !found) {
                String word = scanner.next();    
                if(word.equals("line")) {
                    if(scanner.hasNext()) {
                        String nextWord = scanner.next().replaceAll("[^\\d]", "");
                        if(nextWord.chars().allMatch(Character::isDigit)) {
                            found = true;
                            //I want to stop reading and Just go to the next loop and check for the i occurence
                        }
                    }
                }
            }
      }

答案 2 :(得分:0)

您希望在while周围添加一个if statements循环,该循环将永久地继续,或者在我添加时,退出选项(4)。还要在循环结束时添加第二个input prompt以重新选择它们以进行公式选择。

print("""
Welcome to the Science Speed, Distance, and Time Formula Caculator!

[1] Speed
[2] Distance
[3] Time
[4] Quit

Enter the number NOT the word.
""")
choice1 = int(input("Choose a Formula: "))
while(choice1 != '4'):
    if choice1 == 1:
        print("You have choosen the Speed Formula!")
        D = int(input("Enter the Distance(meters): "))
        T = int(input("Enter the Time(seconds): "))
        Equation = D/T
        print ("Your answer is ",Equation,"m/s.")
    elif choice1 == 2:
        print("You have choosen the Distance Formula!")
        S = int(input("Enter the Speed(m/s): "))
        T = int(input("Enter the Time(seconds): "))
        Equation = S*T
        print("Your answer is ",Equation,"meters.")
    elif choice1 == 3:
        print("You have choosen the Time Formula!")
        D = int(input("Enter the Distance(meters): "))
        S = int(input("Enter the Speed(m/s): "))
        Equation = D/S
        print("Your answer is ",Equation,"seconds.")
    choice1 = int(input("Choose a Formula: "))