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

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

如何隨機(jī)選擇一個(gè)數(shù)學(xué)運(yùn)算符并用它問重復(fù)的數(shù)學(xué)問題?

如何隨機(jī)選擇一個(gè)數(shù)學(xué)運(yùn)算符并用它問重復(fù)的數(shù)學(xué)問題?

交互式愛情 2019-11-20 12:34:57
我有一個(gè)簡(jiǎn)單的數(shù)學(xué)任務(wù),執(zhí)行時(shí)遇到問題,涉及隨機(jī)導(dǎo)入。這個(gè)想法是有10個(gè)隨機(jī)生成的問題的測(cè)驗(yàn)。我使用random.randint函數(shù)獲得的數(shù)字范圍為(0,12),效果很好。選擇隨機(jī)運(yùn)算符的下一個(gè)步驟我遇到了['+','-','*','/']的問題。我在學(xué)校時(shí)有更復(fù)雜的編碼,但這是我的練習(xí),我所需要的就是能夠隨機(jī)創(chuàng)建一個(gè)問題并提出該問題的能力,同時(shí)還能夠自行回答該問題以確定給出的答案是否正確。這是我的代碼:import randomops = ['+', '-', '*', '/']num1 = random.randint(0,12)num2 = random.randint(0,10)operation = random.choice(ops)print(num1)print(num2)print(operation)maths = num1, operation, num2print(maths)截至目前,我的輸出有些混亂。例如:36*(3, '*', 6)顯然,它無法從(3,'*',6)確定答案。我將在另一個(gè)程序中將此操作轉(zhuǎn)換為子例程,但它需要首先工作!如果我做的不是很好,請(qǐng)?jiān)徫?,這是對(duì)我留在學(xué)校的任務(wù)的快速恢復(fù),并且我對(duì)這方面的知識(shí)還很陌生。提前致謝!
查看完整描述

3 回答

?
翻翻過去那場(chǎng)雪

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

您如何制作將操作員的字符(例如'+')映射到操作員(例如operator.add)的字典。然后進(jìn)行采樣,設(shè)置字符串格式并執(zhí)行操作。


import random

import operator

生成隨機(jī)數(shù)學(xué)表達(dá)式


def randomCalc():

    ops = {'+':operator.add,

           '-':operator.sub,

           '*':operator.mul,

           '/':operator.truediv}

    num1 = random.randint(0,12)

    num2 = random.randint(1,10)   # I don't sample 0's to protect against divide-by-zero

    op = random.choice(list(ops.keys()))

    answer = ops.get(op)(num1,num2)

    print('What is {} {} {}?\n'.format(num1, op, num2))

    return answer

詢問用戶


def askQuestion():

    answer = randomCalc()

    guess = float(input())

    return guess == answer

最后進(jìn)行多問題測(cè)驗(yàn)


def quiz():

    print('Welcome. This is a 10 question math quiz\n')

    score = 0

    for i in range(10):

        correct = askQuestion()

        if correct:

            score += 1

            print('Correct!\n')

        else:

            print('Incorrect!\n')

    return 'Your score was {}/10'.format(score)

一些測(cè)試


>>> quiz()

Welcome. This is a 10 question math quiz


What is 8 - 6?

2

Correct!


What is 10 + 6?

16

Correct!


What is 12 - 1?

11

Correct!


What is 9 + 4?

13

Correct!


What is 0 - 8?

-8

Correct!


What is 1 * 1?

5

Incorrect!


What is 5 * 8?

40

Correct!


What is 11 / 1?

11

Correct!


What is 1 / 4?

0.25

Correct!


What is 1 * 1?

1

Correct!


'Your score was 9/10'


查看完整回答
反對(duì) 回復(fù) 2019-11-20
  • 3 回答
  • 0 關(guān)注
  • 719 瀏覽

添加回答

舉報(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)