Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Coin Flip Program
#6
(Sep-24-2017, 09:42 AM)buran Wrote: it IS looping, but you never change the value of toss in the body of the loop

I don't know why my brain tries so hard to make things harder than they need to be. I was already starting to have the realization that it must have something to do with the toss and my loop, but then you confirmed it. After some trial and error, ultimately all I did was add the same line toss = random.randint(1,2) to the loop itself and Boom! problem resolved. For some reason it didn't occur to me that I needed to add that line inside the loop or that I even could add it to get it to work.

Here are new examples.

import random

toss = random.randint(1,2)
heads = 0
tails = 0
count = 0


print(
'''
                Welcome to Coin Toss!
We\'ll flip a coin 100 times and tell you the results.

'''
)
input('\nPress the enter key to continue.')
while count != 100:
    count += 1
    toss = random.randint(1,2)

    if toss < 2:
        print('You got heads!')
        heads += 1
        
    if toss > 1:
        print('You got tails!')
        tails += 1

print('After 100 tosses?...')
print('\nYou got heads', heads, 'times!')
print('\nand You got tails', tails, 'times!')
print('\nThanks for playing Coin Toss!')

input('\n\nPress the enter key to exit.')
Output:
You got heads! You got heads! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got tails! You got heads! You got heads! You got tails! You got tails! You got heads! You got tails! After 100 tosses?... You got heads 47 times! and You got tails 53 times! Thanks for playing Coin Toss! Press the enter key to exit.
Thanks again for the help!! Smile
One day I hope to be able to assist someone else rather than always be the one asking for help.
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply


Messages In This Thread
[split] Coin Flip Program - by Crackity - Sep-24-2017, 09:09 AM
RE: [split] Coin Flip Program - by buran - Sep-24-2017, 09:26 AM
RE: [split] Coin Flip Program - by Crackity - Sep-24-2017, 09:33 AM
RE: [split] Coin Flip Program - by buran - Sep-24-2017, 09:42 AM
RE: [split] Coin Flip Program - by Crackity - Sep-25-2017, 03:48 AM
RE: [split] Coin Flip Program - by Crackity - Sep-25-2017, 02:33 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Are there errors in the code for my coin toss systems? Matlibplot is too perfect . Coolkat 0 411 Nov-13-2023, 11:54 AM
Last Post: Coolkat
  [split] Results of this program in an excel file eisamabodian 1 1,610 Feb-11-2022, 03:18 PM
Last Post: snippsat
  Coin Toss - Distribution lasek723 6 3,185 Oct-04-2020, 01:36 PM
Last Post: deanhystad
  effective means to flip boolean values? Exsul 3 4,402 Aug-25-2019, 03:58 PM
Last Post: Exsul
  [split] Looping a Program volcano63 7 4,173 Oct-09-2018, 12:26 AM
Last Post: micseydel
  Coin Flip Program Warbit 6 8,209 Mar-27-2017, 11:33 PM
Last Post: Warbit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020