Python ( Heads or Tails Counter ) [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to create a simple heads or tails program. For some reason it's not working and I have no idea why. Any ideas?
import random
counter = 0
flip = random.randint(1,2) # Initializing Values of variables
heads_counter = 0
tails_counter = 0
if flip == 1:
heads_counter +=1
counter +=1
elif flip == 2:
tails_counter +=1
counter +=1
else:
print("Invalid flip")
print("Flip is",flip)
print("Flipped The coin",counter,"Times")
print("Landed Heads",heads_counter,"Times")
print("Landed Tails",tails_counter,"Times")

In order to get the numbers to increment, you must have multiple flips during one execution. Here is a much simpler implementation of what you are doing that runs the program num times.
To flip the coin 100 times, simply set num = 100
from random import randint
# number of coin flips to simulate
num = 100
# simulate the coin flips
flips = [randint(1,2) for x in range(num)]
# count up the results
heads = flips.count(1)
tails = flips.count(2)
# print the results to the console
print("{0} Coin Flips".format(num))
print("{0} Heads".format(heads))
print("{0} Tails".format(tails))
This gives you output like the following:
>>> 100 Coin Flips
>>> 49 Heads
>>> 51 Tails

Related

How to compare all elements of a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This code in under is will let the user enter the number of elements of list and the user need enter the elements ( User will need type number_of_elements_of_list times) and then count how many positive number, negative number, zero number. The code is like this:
the_number_of_elements_of_the_list = int(input())
number_list = []
for i in range(1, the_number_of_elements_of_the_list + 1):
number_list.append(int(input()))
positive_number_count = 0
negative_number_count = 0
zero_number_count = 0
if number_list == 0:
zero_number_count += 1
elif number_list == 1 or number_list > 1:
positive_number_count += 1
elif number_list < 0:
negative_number_count += 1
print(positive_number_count, negative_number_count, zero_number_count)
The code have a problem: The list can not compare like that. It will be error but i don't know how to compare the elements of list. Can you help me solve this problem?
Firstly, as teambob pointed out, add indent to the for-loop block.
Secondly, as DarkKnight pointed out, put the count variables outside the for-loop.
Thirdly, for each iteration, in order to use that value alone, use number_list[i-1] instead of number_list. (The index is i-1 rather than i because the range in your code starts from 1 rather than 0)
The final code would look like:
the_number_of_elements_of_the_list = int(input())
positive_number_count = 0
negative_number_count = 0
zero_number_count = 0
number_list = []
for i in range(1, the_number_of_elements_of_the_list + 1):
number_list.append(int(input()))
if number_list[i-1] == 0:
zero_number_count += 1
elif number_list[i-1] == 1 or number_list[i-1] > 1:
positive_number_count += 1
elif number_list[i-1] < 0:
negative_number_count += 1
print(positive_number_count, negative_number_count, zero_number_count)

Writing a Loop to see if a number in a list is even or odd (python) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to write a loop that goes through a list of numbers and prints whether each number is even or odd. It seemed pretty simple, but it just infinitely prints odd. What is the fallacy?
samplenumber = [1,2,3,4,5,6,7,8,9,10]
o = 0
f1 = samplenumber[o]
while f1 < 11:
if f1%2 == 0:
print ("EVEN")
else:
print ("ODD")
o += 1
You have an infinite while loop because you're incrementing the wrong variable. The while loop condition has to equal false at some point, but yours will never equal false (so it keeps running over and over again). See my comment in your code for further explanation:
samplenumber = [1,2,3,4,5,6,7,8,9,10]
o = 0
f1 = samplenumber[o]
while f1 < 11: #f1 will always equal 1 (samplenumber[o] = samplenumber[0] = 1), and 1 is always <11
if f1%2 == 0:
print ("EVEN")
else:
print ("ODD")
o += 1
You should either increment f1 in your while loop or use o as your counter variable.
Have a nice day!
you need to read up on for-loops, friend!
samplenumber = [1,2,3,4,5,6,7,8,9,10]
for i in samplenumber:
if i%2 == 0:
print ("EVEN")
else:
print ("ODD")
best of luck, cheers.
ans:
def test2():
samplenumber = [1,2,3,4,5,6,7,8,9,10]
for item in samplenumber:
if item%2 == 0:
print("EVEN")
else:
print("ODD")
Output:
ODD
EVEN
ODD
EVEN
ODD
EVEN
ODD
EVEN
ODD
EVEN

Counting Heads and Tails in a coin flip program

For my assignment, I have to use Functions within Python to simulate a coin flip. I managed to get the coin flip to showcase heads and tales for the amount the user has inputted. However, for the next portion, I have to get the program to read how many times Heads and Tails appeared. The error I am getting is
'NameError: name 'heads' is not defined'.
import random
def main():
tosses = int(input("Please enter the amount of coin tosses:"))
coin(tosses)
count = 0
heads = 0
tails = 0
def coin(tosses):
for toss in range(tosses):
if random.randint(1, 2) == 1:
print('Heads')
heads += 1
count += 1
else:
print('Tails')
heads += 1
count += 1
print (heads)
print (tails)
main()
Problem
heads was defined out of the scope of the function coin.
Solution
Try defining the variables inside the function, then returning them. Note: the same had to be done with main.
import random
def main():
tosses = int(input("Please enter the amount of coin tosses:"))
coin(tosses)
heads, tails, count = coin(tosses)
return heads, tails
def coin(tosses):
count = 0
heads = 0
tails = 0
for toss in range(tosses):
if random.randint(1, 2) == 1:
print('Heads')
heads += 1
count += 1
else:
print('Tails')
tails += 1 # note you had this say heads before
count += 1
return heads, tails, count
heads, tails = main()
print(heads)
print(tails)
More on why this problem occurred
When you use def and create a new function, all of the variables in that function are accessible just to that function.
What was happening to you, is that you were trying to update the heads variable with heads += 1, but the function literally didn't know what the variable you were referring to is! (That variable was defined in main, and is only accessible from within the main function.)

Roll Two Dice Python 3.5.1 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Instructions: Simulate rolling 2 die with 6 sides each 100 times and count these 3
cases
-The dice sum equals 7
-The 2 die are doubles (same number)
-The dice sum is 10,11, or 12 (greather than or equals to 10)
What I have:
from random import randint
def rolldie():
return randint(1, 7) + randint(1, 7)
n=10
for j in range(n):
print(str(j) + ". Outcome: " + str(rolldie()))`
Overall I don't know if this is correct. Looking for more help. Thank you.
You need to return the values of both dice, not their sum, so you can compare whether they were each the same value.
def roll_dice():
return (random.randint(1,6), random.randint(1,6))
equal_7 = 0
doubles = 0
ten_or_more = 0
for i in range(100):
d1, d2 = roll_dice()
if d1 + d2 == 7:
equal_7 += 1
if d1 == d2:
doubles += 1
if d1 + d2 >= 10:
ten_or_more += 1

How to end a nested loop? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I wrote a script to perform a connectivity search in a binary image (the possible values for the array are 0 and 1). For each pixel, the code looks at how many neighbours have intensity 1, and if there are at least 5 neighbours with I=1, it assigns 1 as a value of the considered pixel. I want the code to repeat the process until no new pixels are assigned intensity 1. At present the code is not performing the connectivity search iteratively; do you have any suggestion on how to fix this?
pixel_count = 0
pixel_counter = [0] * 100
for p in range(1, 100):
if p < 3:
continue
else:
if pixel_counter[p-1] > pixel_counter[p-2]:
continue
else:
break
for q in range(1, ran_x-1):
for r in range(1, ran_y-1):
counter = 0
if neighbours_mask_1[q,r] == 1:
counter = counter +1
if neighbours_mask_2[q,r] == 1:
counter = counter +1
if neighbours_mask_3[q,r] == 1:
counter = counter +1
if neighbours_mask_4[q,r] == 1:
counter = counter +1
if neighbours_mask_5[q,r] == 1:
counter = counter +1
if neighbours_mask_6[q,r] == 1:
counter = counter +1
if neighbours_mask_7[q,r] == 1:
counter = counter +1
if neighbours_mask_8[q,r] == 1:
counter = counter +1
if counter > 5:
mask_1[q,r] = 1
pixel_count = pixel_count + 1
print pixel_count
else:
mask_1[q,r] = 0
pixel_counter[p] = pixel_count
This section of the code:
for p in range(1, 100):
...
if pixel_counter[p-2] > pixel_counter[p-1]:
continue
else:
break
... dead code ...
will slurp all of the execution, the part where I've marked dead code, which contains your counters are never executed because they're unreachable.
I'm not quite sure what your trying to do there.
To answer the question in the title: the easiest way to exit a nested loop is to just move the loops into a function and return from that. E.g.
def f():
for i in range(10):
for j in range(10):
if i + j == 9: return

Categories