Aug-14-2018, 07:25 AM
yet another homework question
yet another homework question
|
Aug-14-2018, 03:42 PM
(This post was last modified: Aug-15-2018, 05:39 AM by HakolYahol.)
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. wait a moment. thank you but what if the for loops are only learned a chapter after?
Aug-16-2018, 09:37 AM
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?
Aug-16-2018, 10:41 AM
(Aug-14-2018, 03:42 PM)HakolYahol Wrote: wait a moment. 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)
Aug-16-2018, 06:49 PM
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
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.")
Sep-27-2018, 04:52 PM
(This post was last modified: Sep-27-2018, 04:52 PM by gruntfutuk.)
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
|
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
Homework help:While Loops question | Midhat_School | 6 | 4,234 |
Jul-26-2020, 10:23 AM Last Post: pyzyx3qwerty |
|
IF ELSE Homework Question | buckthimble | 1 | 2,687 |
Mar-29-2020, 06:29 AM Last Post: buran |
|
Python Homework Question | OrcDroid123 | 1 | 3,081 |
Sep-01-2019, 08:44 AM Last Post: buran |
|
Homework question | dmhhfm | 4 | 14,604 |
Apr-10-2019, 07:22 AM Last Post: DeaD_EyE |
|
Beginner Python Homework Question (Calculate Gross Pay) | matchamochi7 | 4 | 6,676 |
Nov-02-2018, 01:06 PM Last Post: buran |
|
python in JES homework question, | lsteffen | 1 | 3,606 |
Feb-11-2018, 05:52 PM Last Post: Taco_Town |
Users browsing this thread: 1 Guest(s)