你如何列出随机数

时间:2016-06-27 18:28:03

标签: python list python-2.7 random numbers

我正在使用python创建一个游戏,计算机将从1到10选择数字,玩家可以选择他们想要保留或删除的数字。随机数的总和必须加起来21和玩家只有10次尝试。我用来创建随机数列表等于21的代码,让玩家删除他们想要的数字。谢谢!!!

我已经开始了一些...我在Mac上使用Python 2.7.5的程序......

import random
import time

name = raw_input("Hello, What's your name?")
print "Welcome," ,name, "Time to play Catch 21!"
time.sleep(1)

tries = 0
tries_remaining = 10
while tries < 10:
          tries += 1
          tries_remaining -= 1

from time import sleep
total = 0

for i in range(10):
    total = total + random()

print total

while True:
    print "Your card is"
    values = ["2","3","4","5","6","7","8","9","10"]
    print(random.randint(1,10))
    print"Do you want to keep or delete this card?"
    card = raw_input("K = Keep.  D = Delete. ")
    from time import sleep
    time.sleep(1)

2 个答案:

答案 0 :(得分:0)

您可以创建随机值列表,只需将结果调用random.randintappend即可。一个人为的例子:

import random
values = []  # list to hold accepted values
for _ in range(10):  # aka "do the following ten times"
    card_value = random.randint(1,10)  # draw a number
    values.append(card_value)  # store it on the list
    # or just 'values.append(random.randint(1,10))'
# values is now something like [1, 3, 2, 4, 1, 10, 8, 5, 9, 10]

现在,您希望有条件地执行此操作。所以,让我们添加玩家互动:

import random
values = []  # list to hold accepted values
# give the player 10 tries
for _ in range(10):  # aka "do the following ten times"
    card_value = random.randint(1, 10)  # pick a card and remember it, we need it several times
    print("Draw: %2d. Do you want to keep or delete this card?" % card_value)  # show card
    keep_delete = raw_input("K = Keep.  D = Delete.")
    if keep_delete == "K":  # wants to keep the card...
      values.append(card_value)  # ...so we store it on the list
      print("Added %2d." % card_value)
    else:  # let's ignore checking ALL variants of wrong input in this example
      print("Discarded %2d." % card_value)
    print("Current sum: %2d" % sum(values))  # sum just sums up all values in values

# At this point, the player has all his/her cards
# check whether player won
if sum(values) == 21:
   print("You are winner!!!")
else:
   print("You lost :(")

您可以执行更多流量控制,例如检查循环内部是否为sum(values) > 21,然后使用break提前退出。

此外,如果您想拥有固定数量的每张卡,您必须实际限制所发卡。以下方法可行:

import random
available_values = list(range(1, 11))  # all integers from 1 to 10
accepted_values = []  # list to hold accepted values
while available_values:  # aka "do the following as long as we have cards"
    card_value = available_values.pop(random.randint(len(available_values)))  # pop removes the chosen card
    values.append(card_value)  # store it on the list
# values is now something like [1, 3, 2, 4, 7, 6, 8, 5, 9, 10]

答案 1 :(得分:0)

所以我修复了你的代码:

import random
import time
# name = raw_input("Hello, What's your name?")
# changed this to this:
name = raw_input("Hello, What's your name? ")
print "Welcome," ,name, "Time to play Catch 21!"
time.sleep(1)
tries = 0
# tries_remaining = 10
# you don't need this it's enough to have tries, and tries_remaining can
# be calculated easily at any time, just use 10-tries instead

# you have while loop, than for loop and then again while loop,
# but you need to have only one loop and i think it's best to
# use while loop
'''
while tries < 10:
          tries += 1
          tries_remaining -= 1
from time import sleep
total = 0
for i in range(10):
    total = total + random()
print total
while True:
    print "Your card is"
    values = ["2","3","4","5","6","7","8","9","10"]
    print(random.randint(1,10))
    print"Do you want to keep or delete this card?"
    card = raw_input("K = Keep.  D = Delete. ")
    from time import sleep
    time.sleep(1)
'''
# you need to define total before while loop
total = 0
# so I'll copy first and second row
while tries < 10:
    tries += 1
# like I said you don't need tries_remaining
# also you don't need from time import sleep because you 
# already have import time
#       by the way when you use:
#           import time     then you need to use:       time.sleep(2)
#       but when you use:
#           from time import sleep      then you need to use:      sleep(2) 
#       instead of 2 you can use any other number of second
# so total is already defined and now you have to randomize card
# you need to save that value because you will need it later
    card = random.randint(1,10)
# same way of importing time applies to importing any module
# so you can use import random or from random import randint
# now you print card and total
    print "Your card is", str(card) + ", you have", total, "in total"
# you ask user to keep it or delete it
    print"Do you want to keep or delete this card?"
    answer = raw_input("K = Keep.  D = Delete. ")
# what you are missing in your code is to check what is answer
# and to do what user have chosen
    if answer == "K":
        # you need to add card to total
        total += card
# if answer is "D" you don't have to do anything
# at the end we can use time.sleep, although I think there is no purpose of it
    time.sleep(1)

# at the end we can print the score (total)
print "End of game, you have", total, "in total."

这里是没有评论的代码:

import random
import time
name = raw_input("Hello, What's your name? ")
print "Welcome," ,name, "Time to play Catch 21!"
time.sleep(1)
tries = 0
total = 0
while tries < 10:
    tries += 1
    card = random.randint(1,10)
    print "Your card is", str(card) + ", you have", total, "in total"
    print"Do you want to keep or delete this card?"
    answer = raw_input("K = Keep.  D = Delete. ")
    if answer == "K":
        total += card
    time.sleep(1)
print "End of game, you have", total, "in total."