【问题标题】:I want to generate random numbers then do the addition operation on them我想生成随机数,然后对它们进行加法运算
【发布时间】:2017-12-02 16:42:20
【问题描述】:

我是一个 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)
    

【问题讨论】:

  • 您收到的错误或遇到的问题是什么?
  • 你想用这条线做什么? levelOne = print ("level 1")
  • PyGame 在你的问题中在哪里?你为什么设置标签'pygame'?
  • 我想用那条线打印第 1 层并存储它
  • @PranayKumar 这样做不会存储值。

标签: python python-3.x python-2.7


【解决方案1】:

干得好 9 岁的孩子,你做得很好! 如果我清楚地理解了您的问题,您想要求用户在生成这 5 个随机数后输入另一个字符。如果它有字符 '+' ,那么你应该打印所有这些随机数的总和,你可以重新使用你自己的代码来实现这一点:

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

【讨论】:

  • 感谢它对我有用,正如我爸爸总是说我并不孤单......谢谢大家,我将在下面发布完整代码
【解决方案2】:

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

if-statement之后,您需要将所有数字和print相加。

否则,您的代码将按原样运行。您可能想在input() 中插入一个提示参数,如下所示:

choice = input('Choose a level: ')

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    相关资源
    最近更新 更多