【问题标题】:select a random variable within the list在列表中选择一个随机变量
【发布时间】:2014-09-22 07:52:20
【问题描述】:

我这里有一组问题我需要为用户随机选择,如果用户失去了等待时间是需要的

decades = 100
ans = 0

ans = int (input ("halves of 40 =  "))
if ans == 20:
    print ans, " is a right ans good job!"
else:
    print ans, "is a Wrong ans best of luck for next quation."
ans = int (input ("halves of 10 =  "))
if ans == 5:
    print ans, " is a right ans good job!"
else:
    print ans, "is a Wrong ans best of luck for next quation."
ans = int (input ("halves of 90 =  "))
if ans == 45:
    print ans, " is a right ans good job!"
else:
    print ans, "is a Wrong ans best of luck for next quation."
ans = int (input ("halves of 20 =  "))
if ans == 10:
    print ans, " is a right ans good job!"
else:
    print ans, "is a Wrong ans best of luck for next quation."
ans = int (input ("halves of 80 =  "))
if ans == 40:
    print ans, " is a right ans good job!"
else:
    print ans, "is a Wrong ans best of luck for next quation."
ans = int (input ("halves of 30 =  "))
if ans == 15:
    print ans, " is a right ans good job!"
else:
    print ans, "is a Wrong ans best of luck for next quation."

ans = int (input ("halves of 100 =  "))
if ans == 50:
    print ans, " is a right ans good job!"
else:
    print ans, "is a Wrong ans best of luck for next quation."

ans = int (input ("halves of 60 =  "))
if ans == 30:
    print ans, " is a right ans good job!"
else:
    print ans, "is a Wrong ans best of luck for next quation."
ans = int (input ("halves of 50 =  "))
if ans == 25:
    print ans, " is a right ans good job!"
else:
    print ans, "is a Wrong ans best of luck for next quation."
ans = int (input ("halves of 70 =  "))
if ans == 35:
    print ans, " is a right ans good job!"
else:
    print ans, "is a Wrong ans best of luck for next quation."

【问题讨论】:

标签: python random


【解决方案1】:

你可能想做这样的事情:

import random
tries = 10
questions = [("halves of 40 =  ",20),
             ("halves of 10 =  ",5),
             ("halves of 90 =  ",45),
             ("halves of 20 =  ",10),
             ("halves of 80 =  ",40),
             ("halves of 30 =  ",15),
             ("halves of 100 =  ",50)]

for i in xrange(tries):
    q = random.choice(questions)
    ans = int (input (q[0]))
    if ans == q[1]:
        print ans, " is a right ans good job!"
    else:
        print ans, "is a Wrong ans best of luck for next quation."

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    相关资源
    最近更新 更多