Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 你如何列出随机数_Python_List_Python 2.7_Random_Numbers - Fatal编程技术网

Python 你如何列出随机数

Python 你如何列出随机数,python,list,python-2.7,random,numbers,Python,List,Python 2.7,Random,Numbers,我正在与python合作创建一个游戏,计算机将从1到10中选择数字,玩家可以选择他们想要保留或删除的数字。随机数字的总和必须达到21,玩家只有10次尝试。我使用什么代码来创建等于21的随机数列表并让玩家删除他们想要的号码。谢谢 我已经开始做了一些…我在Mac上使用Python2.7.5的程序 import random import time name = raw_input("Hello, What's your name?") print "Welcome," ,name, "Time t

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

我已经开始做了一些…我在Mac上使用Python2.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)
随机导入
导入时间
name=原始输入(“你好,你叫什么名字?”)
打印“欢迎”,姓名,“是时候玩第21条了!”
时间。睡眠(1)
尝试=0
尝试剩余=10
当尝试<10时:
尝试次数+=1
尝试u剩余-=1
从时间上导入睡眠
总数=0
对于范围(10)内的i:
总计=总计+随机()
打印总数
尽管如此:
打印“您的卡是”
值=[“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”]
打印(随机.随机数(1,10))
打印“您想保留或删除此卡吗?”
卡片=原始输入(“K=保留。D=删除”)
从时间上导入睡眠
时间。睡眠(1)

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

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]

因此,我已按如下方式修复您的代码:

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."
随机导入
导入时间
#name=原始输入(“你好,你叫什么名字?”)
#将此更改为:
name=原始输入(“你好,你叫什么名字?”)
打印“欢迎”,姓名,“是时候玩第21条了!”
时间。睡眠(1)
尝试=0
#尝试剩余=10
#你不需要这个,试一下就足够了,试一下就可以了
#任何时候都可以轻松计算,只需尝试10次即可
#你有while循环,而不是for循环,然后又是while循环,
#但是你只需要有一个循环,我认为最好是
#使用while循环
'''
当尝试<10时:
尝试次数+=1
尝试u剩余-=1
从时间上导入睡眠
总数=0
对于范围(10)内的i:
总计=总计+随机()
打印总数
尽管如此:
打印“您的卡是”
值=[“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”]
打印(随机.随机数(1,10))
打印“您想保留或删除此卡吗?”
卡片=原始输入(“K=保留。D=删除”)
从时间上导入睡眠
时间。睡眠(1)
'''
#您需要在while循环之前定义total
总数=0
#所以我将复制第一行和第二行
当尝试<10时:
尝试次数+=1
#就像我说的,你不需要留下来
#此外,你不需要从时间导入睡眠,因为你
#已经有导入时间了
#顺便说一下,当您使用:
#导入时间,然后需要使用:time.sleep(2)
#但当您使用:
#从导入睡眠开始,您需要使用:sleep(2)
#您可以使用任何其他秒数而不是2
#所以总数已经定义好了,现在你们必须随机化卡片
#您需要保存该值,因为以后需要它
card=random.randint(1,10)
#导入时间的方法同样适用于导入任何模块
#因此,您可以使用import random或from random import randint
#现在你打印卡片和总数
打印“您的卡是”,str(卡)+“,您有”,总计,“总计”
#您要求用户保留或删除它
打印“您想保留或删除此卡吗?”
答案=原始输入(“K=保留。D=删除”)
#代码中缺少的是检查答案是什么
#以及做用户选择的事情
如果答案=“K”:
#您需要将卡添加到总数中
总数+=信用卡
#如果答案是“D”,你不必做任何事情
#最后我们可以利用时间。睡眠,尽管我认为它没有任何意义
时间。睡眠(1)
#最后,我们可以打印分数(总计)
打印“游戏结束,你有”,总计,“总计”
下面是没有注释的代码:

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."
随机导入
导入时间
name=原始输入(“你好,你叫什么名字?”)
打印“欢迎”,姓名,“是时候玩第21条了!”
时间。睡眠(1)
尝试=0
总数=0
当尝试<10时:
尝试次数+=1
card=random.randint(1,10)
打印“您的卡是”,str(卡)+“,您有”,总计,“总计”
打印“您想保留或删除此卡吗?”
答案=原始输入(“K=保留。D=删除”)
如果答案=“K”:
总数+=信用卡
时间。睡眠(1)
打印“游戏结束,你有”,总计,“总计”

欢迎来到stackoverflow!请花点时间去看一看。由于编写的代码毫无意义,您使用
time.sleep()
但从不
import time
,您尝试调用无效的模块
random()
,并且您从不使用数字21执行任何操作,也不使用
变量…我知道代码只完成了一部分,但请提供一个能代表您所面临问题且没有大量不相关和未使用噪音的参数。您将导入多少次
时间
睡眠