Python Forum
yet another homework question
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
yet another homework question
#11
(Aug-14-2018, 05:14 AM)perfringo Wrote: Thank you for advice! I should simplify my coding

for i in range(100):
Reply
#12
i'm reading your responses now
it will take me time to answer back

for now
thank you :)

(Aug-13-2018, 08:51 PM)Fab95 Wrote: I agree that you should try harder by yourself. Yet I'm learning too and specially trying hard to manage loops. Although I think this exercise was not really hard to solve.
import random
heads=0
i=0
for i in range(99):
  i+= 1
  heads_count = random.randrange(2)
  if heads_count == 0:
    heads_count = 0
  elif heads_count == 1:
    heads_count = 1
    heads+=1

print("Number of heads", heads, ".  Number of tails", 100 - heads)

wait a moment.
thank you
but what if the for loops are only learned a chapter after?
Reply
#13
As a beginner myself, I will ask that if you were not yet taught about the for loop until the next chapter, were you taught the while loop already, while True: is a favorite way to repeat something again and again, were you taught how to count and keep a running total for three different items such as heads and tails and total coin tosses, were you taught how to use an if statement, a nested if statement, an elif statement and a break statement after reaching the end of the program? These are the minimum items I can think of without using a for loop. You actually may not need to count tails as total tosses minus heads implies tails. I don't think they want you to copy / paste a simple code 100 times and keep count along the way. Do you know how to import Random, was that taught also? What are your thoughts of the book now?
Reply
#14
(Aug-14-2018, 03:42 PM)HakolYahol Wrote: wait a moment.
thank you
but what if the for loops are only learned a chapter after?

The while True solution would be the following:
import random
count=0
heads=0
while count<99:
  count+= 1
  heads_count = random.randrange(2)
  if heads_count == 0:
    heads_count = 0
  elif heads_count == 1:
    heads_count = 1
    heads+=1
 
print("Number of heads", heads, ".  Number of tails", 100 - heads)
Reply
#15
To make the code happy as to PEP8 style:
import random


count = 0
heads = 0
while count < 99:
    count += 1
    heads_count = random.randrange(2)
    if heads_count == 0:
        heads_count = 0
    elif heads_count == 1:
        heads_count = 1
        heads += 1

print("Number of heads", heads, ".  Number of tails", 100 - heads)
running pycodestyle on that produces no warnings, (which I named fab95.py)
(venv) Larz60p@linux-nnem: src:$pycodestyle fab95.py
(venv) Larz60p@linux-nnem: src:$
on original code:
(venv) Larz60p@linux-nnem: src:$pycodestyle fab95.py
fab95.py:2:6: E225 missing whitespace around operator
fab95.py:3:6: E225 missing whitespace around operator
fab95.py:4:12: E225 missing whitespace around operator
fab95.py:5:3: E111 indentation is not a multiple of four
fab95.py:5:8: E225 missing whitespace around operator
fab95.py:6:3: E111 indentation is not a multiple of four
fab95.py:7:3: E111 indentation is not a multiple of four
fab95.py:9:3: E111 indentation is not a multiple of four
fab95.py:11:10: E225 missing whitespace around operator
fab95.py:12:1: W293 blank line contains whitespace
fab95.py:13:67: W292 no newline at end of file
(venv) Larz60p@linux-nnem: src:$
pycodestyle can be installed with pip:
pip install pycodestyle
Reply
#16
Does this work?

import random

# set the coin
headsCount, tailsCount, count = 0, 0, 0

# the loop
count = 0
while count<100: 
    coin = random.randrange(2)
    if coin == 0:
         headsCount += 1
    else:
        tailsCount += 1
    count += 1

print ("The number of heads was", headsCount)
print ("The number of tails was", tailsCount)

input("\n\nPress the enter key to exit.")
Reply
#17
Simplified, but still using while instead of for

from random import randint
count, heads = 0, 0
while count < 100:
    heads += randint(0, 1)
    count += 1
print('Heads', heads)
print('Tails', 100 - heads)
var += 1 is the same as var = var + 1 (well, technically, it isn't but you don't need to worry about that here)
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework help:While Loops question Midhat_School 6 3,082 Jul-26-2020, 10:23 AM
Last Post: pyzyx3qwerty
  IF ELSE Homework Question buckthimble 1 2,147 Mar-29-2020, 06:29 AM
Last Post: buran
  Python Homework Question OrcDroid123 1 2,370 Sep-01-2019, 08:44 AM
Last Post: buran
  Homework question dmhhfm 4 13,354 Apr-10-2019, 07:22 AM
Last Post: DeaD_EyE
  Beginner Python Homework Question (Calculate Gross Pay) matchamochi7 4 5,678 Nov-02-2018, 01:06 PM
Last Post: buran
  python in JES homework question, lsteffen 1 3,004 Feb-11-2018, 05:52 PM
Last Post: Taco_Town

Forum Jump:

User Panel Messages

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