簡體   English   中英

如何把選擇再試一次重復循環?

[英]How to put a choice to try again to repeat the loop?

因此,在完成代碼后,我希望有一個選項,用戶想通過鍵入 Y/n 或 N/n 再試一次。 我該怎么做?

a=int(input('enter value of n: '))
i = 1
sum=0
   


while a < 1 or a > 300: 
    print(a, 'is not in the range 1-300') 
    exit()

for a in range(1,a+1):
     print (a, end = '  ') 
while i<=a:
    if i%3==0 or i%5==0:
      sum=sum+i
    i=i+1
print('\nsum of all multiples of 3 and 5 is:',sum)

repeat=str(input('Would you like to try again? Y/N?'))


           
          



   

    
  


  

這是一種簡單的方法(保持您的代碼結構),無需任何功能或行話(對於初學者:]):

from sys import exit # Just something to exit your code


repeat = 'y'    # So it starts the first time
while repeat.lower() == 'y':    # repeat.lower() -> convert the string 'repeat' to lowercase, incase user inputs 'Y'. you could also use the or keyword here though
    n = int(input("Enter value of n:\n>>> "))

    if n < 1 or n > 300:
        print("'n' must be between 1 - 300, not " + n) # You could use f-strings, check them out!
        exit()

    sum_of_multiples = 0

    # I'm combining your for and while loop as shown below
    for i in range(1, n + 1):   # Don't name your 'i' and 'n' variables the same -> you did so with 'a'

        print(i, end=', ')  # There are cleaner ways to do this but I'm assuming this is what you want

        if i % 3 == 0 or i % 5 == 0:
            sum_of_multiples += i


    # Printing output
    print(f"\nSum of all multiples of 3 and 5 is: {sum_of_multiples}")  # This is called an f-string

    repeat = input("\nDo you want to go again?\n>>> ")  # If you don't input Y/y, the outer while loop will break and terminate


print("Thank you for running me!")  # Prints after the program finishes running

請注意,在檢查n是否在 1 - 300 之間時不需要exit() ,也可以只使用break

EDIT (WITH BREAK)程序沒有from sys import exit下面:

repeat = 'y'    # So it starts the first time

while repeat.lower() == 'y':    # repeat.lower() -> convert the string 'repeat' to lowercase, incase user inputs 'Y'. you could also use the or keyword here though
    n = int(input("Enter value of n:\n>>> "))

    if n < 1 or n > 300:
        print("'n' must be between 1 - 300, not " + n) # You could use f-strings, check them out!
        break # This will mean that "Thank you for running me!" gets printed even if your input is wrong

    sum_of_multiples = 0

    # I'm combining your for and while loop as shown below
    for i in range(1, n + 1):   # Don't name your 'i' and 'n' variables the same -> you did so with 'a'

        print(i, end=', ')  # There are cleaner ways to do this but I'm assuming this is what you want

        if i % 3 == 0 or i % 5 == 0:
            sum_of_multiples += i


    # Printing output
    print(f"\nSum of all multiples of 3 and 5 is: {sum_of_multiples}")  # This is called an f-string

    repeat = input("\nDo you want to go again?\n>>> ")  # If you don't input Y/y, the outer while loop will break and terminate


print("Thank you for running me!")  # Prints after the program finishes running

希望這有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM