在循环中使用random.choice。

huangapple go评论63阅读模式
英文:

Using random.choice in a loop

问题

import random

flips = 0
sides = ["blue", "blue", "blue", "blue", "yellow", "blue"]

while True:
    side = random.choice(sides)
    if side == "blue":
        print(f"you flipped a blue")
        flips += 1
    elif side == "yellow":
        print(f"you flipped a yellow!")
        flips += 1
        break

输出结果应该是随机的,每次执行都有可能是蓝色或黄色,并且会告诉你进行了多少次翻转。

英文:

I'm new to learning Python, currently learning Modules.
I'm stuck on this:

  1. Randomly choose an element until you choose "yellow", incrementing flips each time
  2. Print a message each time including how many flips have been executed and what the result was
import random 

flips = 0
sides = ["blue", "blue", "blue", "blue", "yellow", "blue"]

side = random.choice(sides) 

for side in sides:
    if side == "blue":
        print("you flipped a blue")
        flips += 1
    elif side =="yellow":
        print("you flipped a yellow!")
        flips += 1
        break

the output i got for this was:

you flipped a blue
you flipped a blue
you flipped a blue
you flipped a blue
you flipped a yellow!
5

I can see here that it's just iterating through my sides list till it gets to "yellow". How do I fix this to actually be random??

答案1

得分: 1

你实际上可以使用 random.shuffle() 来对列表进行随机排序。

代码如下:

import random

flips = 0
sides = ["blue", "blue", "blue", "blue", "yellow", "blue"]

random.shuffle(sides)

for side in sides:
    if side == "blue":
        print("你翻到了蓝色")
        flips += 1
    elif side == "yellow":
        print("你翻到了黄色!")
        flips += 1
        break
英文:

You can actually use random.shuffle() to random a list.

The code would be like this :

import random 

flips = 0
sides = ["blue", "blue", "blue", "blue", "yellow", "blue"]

side = random.shuffle(sides) 

for side in sides:
    if side == "blue":
        print("you flipped a blue")
        flips += 1
    elif side =="yellow":
        print("you flipped a yellow!")
        flips += 1
        break

答案2

得分: 1

如果您熟悉while循环,可以在您的当前代码中使用它。您想要选择随机颜色,直到选择到'yellow'。对于这种特定情况,我建议使用while循环,因为它会重复执行,直到满足某个特定条件。以下是使用while循环满足您要求的更新代码:

import random

flips = 0
sides = ["blue", "blue", "blue", "blue", "yellow", "blue"]

while True:
    side = random.choice(sides)
    flips += 1

    if side == "blue":
        print("You flipped a blue.")
    elif side == "yellow":
        print("You flipped a yellow!")
        break

print("Total flips:", flips)

如果您仍在考虑使用for循环,以下是您可以实现的代码:

import random

flips = 0
sides = ["blue", "blue", "blue", "blue", "yellow", "blue"]

random.shuffle(sides)  # 随机打乱sides列表

for side in sides:
    flips += 1

    if side == "blue":
        print("You flipped a blue.")
    elif side == "yellow":
        print("You flipped a yellow!")
        break

print("Total flips:", flips)

请注意,上述代码均为Python代码,用于模拟选择颜色直到选择到'yellow'的情况。

英文:

If you are familiar with while loop you can use it in your current code. You want to choose random color until 'yellow' had been chosed. For this particular situation I suggest using while loop, because it iterates over until the certain condition is true. Here is updated code which satisfies the requirements you want using while loop.

    import random

    flips = 0
    sides = ["blue", "blue", "blue", "blue", "yellow", "blue"]

    while True:
        side = random.choice(sides)
        flips += 1

        if side == "blue":
            print("You flipped a blue.")
        elif side == "yellow":
            print("You flipped a yellow!")
            break

    print("Total flips:", flips)

If you still considering use of for loop here is the code you can implement:

    import random

    flips = 0
    sides = ["blue", "blue", "blue", "blue", "yellow", "blue"]

    random.shuffle(sides)  # Shuffle the sides list randomly

    for side in sides:
        flips += 1

        if side == "blue":
            print("You flipped a blue.")
        elif side == "yellow":
            print("You flipped a yellow!")
            break

    print("Total flips:", flips)

答案3

得分: 0

你的代码现在将一个随机的面赋给变量 side

side = random.choice(sides)

但在循环中,你通过迭代 sides 列表重新赋值了这个变量。

for side in sides: # 在这里它被重新赋值
    if side == "blue":
        print("你翻到了蓝色")
        flips += 1
    elif side == "yellow":
        print("你翻到了黄色!")
        flips += 1
        break

你所实现的功能与 sides 列表无关。你可以修改你的代码,使用一个 while 循环,因为你只想在特定条件下跳出循环。

while True:
    side = random.choice(sides) # 每次都会重新赋值
    if side == "blue":
        print("你翻到了蓝色")
        flips += 1
    elif side == "yellow":
        print("你翻到了黄色!")
        flips += 1
        break
# 其余部分的代码在这里
英文:

Your code right now assigns a random side to the variable side

side = random.choice(sides) 

But after that in the for loop, you're reassigning the variable by iterating through sides list.

for side in sides: # here it gets reassigned
    if side == "blue":
        print("you flipped a blue")
        flips += 1
    elif side =="yellow":
        print("you flipped a yellow!")
        flips += 1
        break

What you're achieving is independent of the sides list. You can modify your code and using a while loop, as you want to break free of the loop only after a certain condition.

while True:
    side = random.choice(sides) # The side gets reassigned each time
    if side == "blue":
        print("you flipped a blue")
        flips += 1
    elif side =="yellow":
        print("you flipped a yellow!")
        flips += 1
        break
# rest of your code here

huangapple
  • 本文由 发表于 2023年6月5日 23:22:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定