Python使用循环翻转x枚硬币x次,但输出结果奇怪。

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

Python flipping x coins x times using loops but receiving odd output

问题

I see you have a Python code that's meant to simulate coin flips and count the number of heads and tails. I'll provide a translated version of your code for you to better understand it.

import random

heads = 0
tails = 0

coins = int(input("要翻转多少枚硬币?"))
flips = int(input("每枚硬币要翻转多少次?"))

while coins <= 0 or flips <= 0:
    print("输入无效,请重新输入。")
    coins = int(input("要翻转多少枚硬币?"))
    flips = int(input("每枚硬币要翻转多少次?"))

for coin in range(1, coins + 1):
    print(f"硬币 {coin}")

    for flip in range(1, flips + 1):
        result = random.randint(0, 1)
        if result == 0:
            heads += 1
            print(f"翻转:{flip};结果:正面")
        else:
            tails += 1
            print(f"翻转:{flip};结果:反面")

print(f"总共有 {heads} 正面")
print(f"总共有 {tails} 反面")

This code should work as you described. It asks for the number of coins and how many times to flip them, ensures the input is valid, performs the coin flips, and counts the number of heads and tails. The output will be similar to the example you provided.

英文:

I'm new to python and coding in general. I have an assignment for class to write a program that uses a loop to flip multiple coins multiple times. The code must:

  1. ask for the number of coins and how many times to flip those coins.
  2. Must ask again if the input is less then 0.
  3. Print the result of flipping each coin the specified number of times and must be random.
  4. Then print the total number of heads and tails.
    I do have to use random so suggestions of other modules or better ways to achieve randomization aren't something I'm looking for, thank you.

The output should look something like:

How many coins do you want to flip? 2
How many times do you want to flip each coin? 2

Coin 1
Flip: 1; Result: Heads
Flip: 2; Result: Tails

Coin 2
Flip: 1; Result: Tails
Flip: 2; Result: Heads

There were 2 Tails in total
There were 2 Heads in total

Here's what I tried: (I'm having to use my phone due to some irl problems to ask this so if the format is off I'm sorry!)

import random

heads = 0
tails = 0

coins = int(input(&quot;How many coins: &quot;))

while coins !=&quot;&quot;:
    if coins \&lt;= 0:
        print (&quot;Invalid input&quot;)
        coins = int(input(&quot;How many coins: &quot;))
    else:
        flips = int(input(&quot;How many times to flip: &quot;))
        if flips \&lt;= 0:
            print (&quot;Invalid input&quot;)
            flips = int(input(&quot;How many times to flip: &quot;))
        else:
            for n in range (0, coins):
                for i in range (0, flips):
                    print (&quot; &quot;)
                    print (&quot;Coin %0d&quot; %n)
                    print (&quot; &quot;)
                    n = coins + 1
                    for flip in range (0, flips):
                        flip = random.randint(0,1)
                        if flip == 0:
                            heads += 1
                            print (&quot;Flips: %0d;&quot; %i, &quot;Result: heads&quot;)
                        else:
                            tails += 1
                            print (&quot;Flip: %0d;&quot; %i, &quot;Result: tails&quot;)
                        i = flips + 1
print (&quot;total heads:&quot;, heads)
print (&quot;total tails:&quot;, tails)
break

What I get varies wildly by the numbers I input. I might flip 4 coins 3 times and it flips 6 coins. Or 1 coin 3 times and it flips 6 coins. But 1 coin 2 times flips 1 coin 2 times. The numbers counting the coins and flips also don't work right. I get results like:

Coin 0
Flip: 0; Result: Tails
Flip: 3; Result: Heads

Coin 2
Flip: 1; Result: Tails
Flip: 3; Result: Tails.

I'm stumped and at this point all the code looks like abc soup. Any help is appreciated.

答案1

得分: 0

我建议在评估阶段(进行抛硬币操作时)重新检查你的代码流程控制和变量使用。

具体来说,你似乎在两个不同的循环中使用了抛硬币的次数:for i in range(0, flips):for flips in range(0, flips):

另外,第二个循环(for flips in range(0, flips):)正在覆盖你用来存储用户请求的每个硬币抛掷次数的 flips 变量。

英文:

I'd suggest re-looking into your code's flow control and variable usage once you get to the evaluation stage (when you are doing the coin flipping).

Specifically, you appear to be using the number of flips for two distinct loops: for i in range (0, flips): and for flips in range (0, flips):.

Additionally, the second one (for flips in range (0, flips):) is overwriting the flips variable which you are using to store the number of flips per coin that the user requested.

答案2

得分: 0

我之前在这行代码中添加了'for flip in range (0, flips)',没有考虑到移动'for i in range (0, flips)'将向程序传达相同的信息。如果没有Benjamin Davis或Scott Hunter的帮助,我不会发现这一点,非常感谢你们两位!因此,中间部分实际上应该是:

    else:
        for n in range (1, coins +1):
            print (" ")
            print ("Coin %0d" %n)
            print (" ")
            for i in range (1, flips +1):
                flip = random.randint (0,1)
                if flip == 0:
                    heads += 1
                    print ("Flip: %0d;" %i, "Result: heads")
                else:
                    tails += 1
                    print ("Flip: %0d;" %i, "Result: tails")
英文:

I had put in the line 'for flip in range (0, flips)' not thinking that moving the 'for i in range (0, flips)' would tell the program the same thing. Which I wouldn't have figured out without Benjamin Davis or Scott Hunter and I greatly appreciate you both!
So the middle part should actually be:

else:
    for n in range (1, coins +1)
        print (&quot; &quot;)
        print (&quot;Coin %0d&quot; %n)
        print (&quot; &quot;)
        for i in range (1, flips +1)
            flip = random.randit (0,1)
            if flip == 0:
                heads += 1
                print (&quot;Flip: %0d;&quot; %i, &quot;Result: heads&quot;)
            else:
                tails += 1
                print (&quot;Flip: %0d;&quot; %i, &quot;Result: tails&quot;)

huangapple
  • 本文由 发表于 2023年2月19日 10:17:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497605.html
匿名

发表评论

匿名网友

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

确定