简体   繁体   English

如何通过更改随机数进行循环?

[英]How can I make a loop with changing random numbers?

I want to make a game that will generate multipication equations. 我想制作一个会生成乘法方程的游戏。 I want to make it that you will get a simple multipication equation (ex: 5*6) and when you answer it, the program will tell you if you are correct, and then will go to the next random equation. 我想使您得到一个简单的乘法方程(例如:5 * 6),当您回答它时,程序将告诉您是否正确,然后转到下一个随机方程。 I know how to make it with a lot of different random numbers, but this will make the code very long and not ellegant. 我知道如何使用许多不同的随机数进行编码,但这会使代码很长,而且不够雅致。 I need a way to generate two different random numbers every time, without having to make a data base of let's say 20 different random numbers. 我需要一种无需每次创建20个不同随机数的数据库即可每次生成两个不同随机数的方法。 Does anyone know how to do it? 有人知道怎么做吗? Thanks! 谢谢!

This is the code I've wrote so far: 这是我到目前为止编写的代码:

import random
import sys
import os

random_num1 = random.randrange(1, 10)
random_num2 = random.randrange(1, 10)

print(random_num1, 'X', random_num2, '=', )

def multiplication(random_num1, random_num2):
    sumNum = random_num1 * random_num2
    return sumNum

one = input()

if(int(one) == multiplication(random_num1, random_num2)):
    print('true')
else:
    print('false')

Put all of your code into a while loop that never ends using while True: (and don't forget to move everything inside by one tab to the right). 将您所有的代码放入一个while循环,该循环永远不会while True: (并且不要忘记将所有内容向右移一个选项卡)。

It's advisable to move def multiplication out of the while loop though. 最好将def multiplication移出while循环。

You could easily do it with a function and a while loop. 您可以使用一个函数和一个while循环轻松地做到这一点。 Let's say, you want to play 10 times and want the score at the end. 假设您想玩10次,最后想获得比分。

def multiplication(random_num1, random_num2):
    sumNum = random_num1 * random_num2
    # Indentation fixed
    return sumNum

def play() : 
    random_num1 = random.randrange(1, 10)
    random_num2 = random.randrange(1, 10)

    print(random_num1, 'X', random_num2, '=', )

    one = input()

    if(int(one) == multiplication(random_num1, random_num2)):
       return 1
    else:
       return 0

if __name__ == '__main__' :
    number_of_play = 10
    cpt = 0
    result = 0
    while cpt < number_of_play :
        cpt+=1
        cpt += play()
    print "Wow you perform {} out of {}".format(result, number_of_play)

Here it is a simple example made by me (EDITED with "try" statement): 这是我做的一个简单示例(用“ try”语句编辑):

from random import randint

while 1:
    numA = randint(0,9)
    numB = randint(0,9)
    result = numA * numB

    print("\nFirst number:", numA)
    print("Second number:", numB)

    while 1: 
        try: 
            userResult = int(input("Insert your multiplication result: ")) 
            break
        except: 
            print("Error! The given digit is not a number. Retry :")

    if userResult == result:
        print("Bravo! That's right!")
    else:
        print("Damn! That's wrong!")

I've tried to be as clear as possible. 我试图尽可能清楚。 I hope it was useful for you. 希望对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM