亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用戶輸入后如何重新啟動while循環

用戶輸入后如何重新啟動while循環

滄海一幻覺 2022-10-06 19:52:04
我剛剛開始編碼,我正在慢慢理解它。對于我的班級,我們必須制作一個兒童程序來練習他們的數學,并詢問他們是否愿意再試一次,如果它是正確的。如果他們輸入 Y,我無法理解如何讓我的 while True 循環重新啟動。有什么提示嗎?這是我的代碼:    #Addition        A = int(input("What is %i + %i =" %(N1, N2)))        while add != N1 + N2:                 add = int(input("Incorrect, what is %i + %i = " %(N1,N2)))        while add == N1 + N2:                 repeat =(input("Correct! would you like to try again? Y/N "))        if repeat == 'n':                break        if repeat == 'y':                continueif op == "-":    #Subrtraction        s = int(input("What is %i - %i =" %(N1, N2)))        while s != N1 - N2:                 s = int(input("Incorrect, what is %i - %i = " %(N1,N2)))        while s == N1 - N2:                 repeat =(input("Correct! would you like to try again? Y/N "))if op == "*":     #Multiply        m = int(input("What is %i - %i =" %(N1, N2)))        while m != N1 * N2:                 m = int(input("Incorrect, what is %i - %i = " %(N1,N2)))        while m == N1 * N2:                 repeat =(input("Correct! would you like to try again? Y/N "))
查看完整描述

3 回答

?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

我認為最好先進入一個while循環,然后從用戶那里獲取輸入或確定答案是否正確以及其他問題......



# coding: utf-8


# Start of the 1st Question

redo="y"

A1=-1 # can be any integer but not the correct answer

n1, n2 = 2, 3


while (A1 != n1 + n2) or redo.lower()=="y":

    # ask the question

    A1 = int(input("What is the sum of %i and %i : " % (n1, n2)))


    # if answer is correct

    if A1 == n1 + n2:

        redo = input("Correct ! Would you like to try again? (Y/[N]) : ")

        if redo.lower() == "y":

            continue

        else:

            break

    else:

        print("Your Answer : %d is Incorrect!" % A1)


查看完整回答
反對 回復 2022-10-06
?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

這種程序結構沒有意義。


你可以做:


while True:

    num1 = int(input("num1 "))

    num2 = int(input("num2 "))

    op = '+'

    calc = -1


    if op == "+":

        while calc != num1 + num2:

            calc = int( input(f"Whats {num1} {op} {num2}?"))

    elif op == "-":

        pass # code the others 

    elif op == "*":

        pass # code the others 

    elif op == "/":

        pass # code the others - use float where needed instead of int 

        # comparing floats is difficult due to float math rounding imperfection


    print("Correct!" , num1, op, num2,"=",calc)


    again = input("Again? Y  to do it again: ").lower()


    if again != "y": 

        break    # leaves the while True


查看完整回答
反對 回復 2022-10-06
?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

為此,您可以定義一個函數。也許它比你在課堂上做的要復雜一點,但它仍然很基礎,所以你可以輕松學習它:) 使用函數時,請記住在第一次調用它以便它可以工作。這是一個例子:


def function_name():

    while True:

        '''your code'''

        repeat =(input("Correct! would you like to try again? Y/N "))

        if repeat == "y":

            function_name() # wach time the user say "y" the code calls the function again.

        break # the break will finish the while loop and will close the program.


function_name() # that's where I call the function the first time.

順便說一句,如果您正在使用該函數,則實際上不必使用 while 循環。但我認為這是你在課堂上的工作,所以我就這樣吧:)


查看完整回答
反對 回復 2022-10-06
  • 3 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號