简体   繁体   English

如何使非列表循环从1-10中选择4个随机数并显示它们,但它们不能相同?

[英]How to make a non list Loop that will select 4 random numbers from 1-10, and display them, but they cannot be the same?

import random
counter = 0
count = 0
counting = 0
valueOne = 0
valueTwo = 0
while counter ==0:
    playerOne = random.randint(1,10)
    number = random.randint(1,10)

About right here is where I get confused. 关于这里,这让我感到困惑。 It will run the loop, but every time it runs, the random values get reset, and even if the first time it prints a random number, the second time it might print the same and so forth. 它将运行循环,但是每次运行时,随机值都会重置,即使第一次打印随机数,第二次也可能打印相同,依此类推。

    if number == playerOne:
        count = count + 1
    if number != playerOne:
        valueOne = playerOne

        if number != valueOne:

            print("lotto number",number)
            counting = counting + 1
        if counting >= 4:
            print("it took you this many trys",count)
            input('play again?')
            counting = 0
            count = 0

Should list the numbers in 1-10 (inclusive) then randomly sample them. 应列出1-10(含)之间的数字,然后随机抽样。

random.sample(range(1,11), 4)

random.sample docs random.sample文档

range docs 范围文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 循环:计算 1-10 的 2 个随机值之间的总和 - Python loop: calculate sum between 2 random values from 1-10 尝试创建 1-10 之间的 10 个随机数的列表,这些随机数在 python 中不重复 - Trying to create a list of 10 random numbers between 1-10 that do not repeat in python Python:生成数字1-10的列表,但是1最终出现在列表的末尾吗? - Python: Generating list of numbers 1-10, but 1 ends up at the end of the list? 用 10 个随机数填充一个列表,然后随机选择其中的 5 个 - Fill a list with 10 random numbers, then chose 5 of them randomly 使用 for 循环和随机数创建嵌套列表 - make a nested list using a for loop and random numbers 如何生成包含 10 个随机数列表的文件,对它们进行排序并写入新文件 - How to generate a file with a list of 10 random numbers, sort them and write to a new file 你如何列出数字 1-10 前面有东西 - How do you list number 1-10 with something in front of it 如何制作一个随机唯一数字列表,避免使用已经存在的随机唯一数字列表中的数字? - How do i make a list of random unique numbers that avoid using numbers from an already existing list of random unique numbers? 随机的数字列表并添加它们 - Random list of numbers and add them python,从(1,n)中选择随机的#k数字,但列表中的数字除外 - python, select random #k numbers from (1, n) excluding numbers in list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM