Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] python class
#1
Hi guys, I'm totally new - I mean totally new to coding. So I go trough the basics and it already seems like rocket science for me.
I made a code for the COIN FLIPPING game and it gives me random results head/tail only with IF , ELSE function. But once I add the WHILE function and I want it to print 100 results I get 100 heads one time and 100 tails next time ..

Could you please look at it and tell me what is the problem with it...., thanks.

https://gyazo.com/d45fb6f88f08e7edd39940259fa8ef0f
Reply
#2
People round here are a bit leery of clicking random links. Please post your code, in Python tags (see the BBCode link below for instructions), and we will be glad to help you out.

My guess without seeing your code is that the coin flipping command is not inside the loop. You're doing it once before the loop, and it's printing out the same result every time through the loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Aug-19-2018, 01:22 AM)ichabod801 Wrote: People round here are a bit leery of clicking random links. Please post your code, in Python tags (see the BBCode link below for instructions), and we will be glad to help you out.

My guess without seeing your code is that the coin flipping command is not inside the loop. You're doing it once before the loop, and it's printing out the same result every time through the loop.

I see it now - the Python tags - I was wondering how people post the codes....... I'm new to this forum as well :)

So here is my original question .....

Hi guys, I'm totally new - I mean totally new to coding. So I go trough the basics and it already seems like rocket science for me.
I made a code for the COIN FLIPPING game and it gives me random results head/tail only with IF , ELSE function. But once I add the WHILE function and I want it to print 100 results I get 100 heads one time and 100 tails next time ..

Could you please look at it and tell me what is the problem with it...., thanks.

# COIN FLIPPING GAME

import random
x = random.randint(1,2) # 1 = head , # 2 = tail

toss = 0
while toss != 100:
    toss = toss + 1
    if x == 1:
        print('Head')
        
    else:
        print('Tail')
        
        
print('end')
Reply
#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
#5
(Aug-19-2018, 03:33 AM)ichabod801 Wrote: 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.

Thanks so much. I was paying attention to have it indented but I overlooked the x = random.randint(1,2) I already came acroos for loop and I see what you saying...
Thanks again, I'm writing down all your tips.
As I said I just started and it is very helpful to have forum like this.
Is there any "special" thread for very beginners?
I appreciate your help..... thank you
Reply
#6
There's no special area for very beginners. There's the homework section, but sometimes people have homework involving complicated data cleaning and multivariate analysis, so there's not guarantee that it's for beginners.

We do have a tutorials section you might want to look at. Many of the tutorials are tagged as [Basic], meaning they are aimed at new programmers.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Thanks - I will look at it...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class test : good way to split methods into several files paul18fr 4 403 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  [split] Class takes no arguments bily071 2 598 Oct-23-2023, 03:59 PM
Last Post: deanhystad
  [split] Python Class Problem astral_travel 12 4,824 Apr-29-2020, 07:13 PM
Last Post: michael1789
  split by character class Skaperen 3 2,292 Jul-15-2019, 02:29 AM
Last Post: Skaperen
  Converting c++ class to python class panoss 12 11,709 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