第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使用循環(huán)重新運(yùn)行基本計(jì)算器

如何使用循環(huán)重新運(yùn)行基本計(jì)算器

慕運(yùn)維8079593 2021-09-25 14:15:48
我對(duì)編程很陌生,作為一名數(shù)學(xué)學(xué)生,我將學(xué)習(xí) Python 編程。為了做好充分準(zhǔn)備,我認(rèn)為我已經(jīng)深入了解該計(jì)劃,使用一些 YouTube 視頻和在線材料?,F(xiàn)在的問(wèn)題。我正在構(gòu)建一個(gè)基本的計(jì)算器。它適用于我在其中描述的三個(gè)功能。但是如果有人打錯(cuò)了他或她想要使用的函數(shù)(例如輸入“multifly”iso“multiply”),它會(huì)返回一個(gè)句子,告訴用戶它打錯(cuò)了。我想代表這條線,但也讓它從頭開(kāi)始重新運(yùn)行。也就是說(shuō),如果你打錯(cuò)了,回到第 1 行,詢問(wèn)用戶他想做什么。我知道我必須使用 for 或 while 循環(huán),但我不知道如何實(shí)際使用它,工作。請(qǐng)給我一些建議:)choice = input("Welcome to the basic calculator, please tell me if you want to add, substract or muliply: ")if choice == "add":     print("You choose to add")     input_1 = float(input("Now give me your first number: "))     input_2 = float(input("And now the second: "))     result = (input_1 + input_2)     if (result).is_integer() == True:          print("Those two added makes " + str(int(result)))     else:          print("Those two added makes " + str(result))elif choice == "substract":     print("You choose to substract")     input_1 = float(input("Now give me your first number: "))     input_2 = float(input("And now the second: "))     result = (input_1 - input_2)     if (result).is_integer() == True:          print("Those two substracted makes " + str(int(result)))     else:         print("Those two substracted makes " + str(result))elif choice == "multiply":     print("You choose to multiply")     input_1 = float(input("Now give me your first number: "))     input_2 = float(input("And now the second: "))     result = (input_1 * input_2)     if (result).is_integer() == True:         print("Those two multiplied makes " + str(int(result)))     else:         print("Those two multiplied makes " + str(result))else:print("I think you made a typo, you'd have to try again.")
查看完整描述

3 回答

?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

choices={"add","substract","multiply"}

while 1:

    choice = input("Welcome to the basic calculator, please tell me if you want to add, substract or muliply: ")

    if choice not in choices:

        print("I think you made a typo, you'd have to try again.")

    else: break


if choice == "add":

    ...

你在循環(huán)中完成輸入和驗(yàn)證部分,然后你的代碼進(jìn)行計(jì)算。(假設(shè)計(jì)算器只計(jì)算一次然后退出。如果沒(méi)有,你可以把整個(gè)事情放在另一個(gè)循環(huán)中進(jìn)行更多計(jì)算,也許有一個(gè)退出命令。)


在我的示例中,驗(yàn)證是通過(guò)包含您可能的命令的集合(選擇)完成的,并檢查輸入的成員資格。


查看完整回答
反對(duì) 回復(fù) 2021-09-25
?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊

OP = ("add", "subtract", "multiply")


while True:

    choice = input("Pick an operation {}: ".format(OP))

    if choice not in OP:

        print("Invalid input")

    else:

        break


if choice == OP[0]:

#...


查看完整回答
反對(duì) 回復(fù) 2021-09-25
  • 3 回答
  • 0 關(guān)注
  • 305 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)