我想生成随机数,然后对它们进行加法运算

2024-10-03 15:24:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个9岁的孩子,正在尝试将随机数数学自动化到我的算盘上。你知道吗

我想随机生成值,并将它们存储在内存中,以备以后按检查加法时使用。然后它应该给出所有随机数的加法。你知道吗

到目前为止我有这个密码

# my abacus training app

import random
import time

print ('hey what\'s your name')
name = input()

print("well today i am goingto ask abacus questions ")

print ("choose a level")
levelOne = print ("level 1")
print (levelOne)

choice = input()

if choice.endswith("1") :
    print("let's start")
    noone = random.randint(1,10)
    print (noone)
    time.sleep(5)
    notwo = random.randint(1,10)
    print (notwo)
    time.sleep(5)
    nothree = random.randint(1,10)
    print (nothree)
    time.sleep(5)
    nofour = random.randint(1,10)
    print (nofour)
    time.sleep(5)
    nofive = random.randint(1,10)
    print (nofive)
    time.sleep(5)

=======我发布了上述问题的最终答案,这是我的最终代码,感谢Srig和Hamed Temsah的输入===感谢所有支持我的人。你知道吗

# my abacus training app import random import time print ('hey what\'s your name') name = input() print("well today i am goingto ask abacus questions ") print ("choose a level") choice = input("choose a level : ") if choice.endswith("1") : print("let's start") noone = random.randint(1,10) print (noone) time.sleep(10) notwo = random.randint(1,10) print (notwo) time.sleep(10) nothree = random.randint(1,10) print (nothree) time.sleep(10) nofour = random.randint(1,10) print (nofour) time.sleep(10) nofive = random.randint(1,10) print (nofive) time.sleep(10) print ("click enter to check your ans") input () print(noone+notwo+nothree+nofour+nofive)

Tags: nameimportinputtimesleeprandomlevelprint
2条回答

你可以不用levelOne = print ("level 1")而只做print("level 1"),因为print()返回None。要确认这一点,请尝试levelOne == None。你知道吗

if-statement之后,需要将所有数字和print相加。你知道吗

否则,代码将按原样工作。您可能希望在input()中插入提示参数,如下所示:

choice = input('Choose a level: ')

希望有帮助。你知道吗

干得好9岁的孩子,你干得好! 如果我明白你的问题,你想让用户输入另一个字符后,生成这5个随机数。如果它有字符“+”,那么您应该打印所有这些随机数的总和,您可以重新使用自己的代码来实现这一点,如:

operand = input()
if operand.endswith("+") :
    print(noone+notwo+nothree+nofour+nofive)

相关问题 更多 >