Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] python class
#4
As I thought. Your call to random.randint(1, 2) is what flips the coin. But that is not part of your loop. To be part of the loop it needs to be part of the indented block of code under the while statement.

About the while statement. While loops are for when you're not sure how many times through the loop you'll want to go. When you know how many times through the loop you want to go, use a for loop. In your case you would want for toss in range(100):. Then you don't need the other lines that keep track of toss. The for loop will keep track of toss for you.

There's also another function in random that you might be interested in: choice. x = random.choice(['Heads', 'Tails']) will randomly pick either 'Heads' or 'Tails' and put it in x. Then you can just print x, and you don't need the if/else bit.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
[split] python class - by JPan - Aug-19-2018, 12:35 AM
RE: [split] python class - by ichabod801 - Aug-19-2018, 01:22 AM
RE: [split] python class - by JPan - Aug-19-2018, 01:57 AM
RE: [split] python class - by ichabod801 - Aug-19-2018, 03:33 AM
RE: [split] python class - by JPan - Aug-19-2018, 06:13 AM
RE: [split] python class - by ichabod801 - Aug-19-2018, 12:55 PM
RE: [split] python class - by JPan - Aug-19-2018, 04:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class and methods ebn852_pan 9 471 May-20-2024, 08:46 PM
Last Post: ebn852_pan
  Class test : good way to split methods into several files paul18fr 4 579 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  [split] Class takes no arguments bily071 2 688 Oct-23-2023, 03:59 PM
Last Post: deanhystad
  [split] Python Class Problem astral_travel 12 5,117 Apr-29-2020, 07:13 PM
Last Post: michael1789
  split by character class Skaperen 3 2,409 Jul-15-2019, 02:29 AM
Last Post: Skaperen
  Converting c++ class to python class panoss 12 11,990 Jul-23-2017, 01:16 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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