I am wondering where my code is going wrong [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
import random
def coinToss():
number = input("Number of times to flip coin: ")
recordList = []
heads = 0
tails = 0
for amount in range(number):
flip = randint(0, 1)
if: (flip == 0):
print("Heads")
recordList.append("Heads")
else:
print("Heads")
recordList.append("Tails")
print(str(recordList))
print(str(recordList.count("Heads")) + str(recordList.count("Tails")))
I am wondering what is going wrong here. it says there is a syntax error in the if statement. I am just trying to make a basic coin flip app.
Ill put my error here:
{
"resource": "/c:/Users/liamd/Documents/Programming
Files/Python/Cointoss.py",
"owner": "python",
"code": "syntax-error",
"severity": 8,
"message": "invalid syntax (<unknown>, line 10)",
"source": "pylint",
"startLineNumber": 10,
"startColumn": 1,
"endLineNumber": 10,
"endColumn": 1
}

I fixed your code and it came out to this.
import random
def coinToss():
number = input("Number of times to flip coin: ")
recordList = []
heads = 0
tails = 0
for amount in range(int(number)):
flip = random.randint(0, 1)
if flip == 0:
print("Heads")
heads += 1
recordList.append("Heads")
else:
print("Tails")
tails += 1
recordList.append("Tails")
return recordList
list = coinToss()
print("Heads: "+str(list.count("Heads")), "Tails: "+str(list.count("Tails")))
There were a lot of errors, and you should look into it before posting it on Stack Overflow

Remove ":" after "if", put it only at the end of the line.
So:
if (condition):
do something
else:
do something else

Related

TypeError: 'int' object is not callable (While working on google colaboratory) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
In python I got a "TypeError: 'int' object is not callable" during execution.
I had read other posts but I still can't figure out why it is like this.
# Python code to find if a number is
# prime or not using divmod()
# Given integer
n = int(input("Enter a number"))
x = n
# Initialising counter to 0
count = 0
while x != 0:
p, q = divmod(n, x)
x -= 1
if q == 0:
count += 1
if count > 2:
print(n, 'is Not Prime')
else:
print(n, 'is Prime')
And, please note that it doesn't gives any error by replacing:
n = int(input("Enter a number")) to n = int(input("Enter a number"))
I have also provided a screenshot regarding my problem 👇
image
Answer if anybody knows, Appreciation for any suggestions and comments.
Have you tried something like this?
z = input("Enter a number")
n = int(z)
x = n

How to fix a while syntax error in python? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm getting a syntax error on my 2nd while loop. Can't figure out why, any help appreciated :)
#intro
print("Welcome to my prime number detector.")
print("Provide an integer and I will determine if it is prime.")
#again loop
again = "Y"
while again == "Y":
num = (int(input("Enter an integer"))
#check for valid input
while num < 1:
num = (int(input("Enter an integer"))
#test for prime
for d in range(2,num):
if (num % d) == 0:
print(num,"is not prime.")
else:
print(num,"is prime.")
#ask again
again = intput("Do you want to play again? (Y/N)")
You are missing a closing parenthesis ) in the two of your following lines. The correct line of code is
num = (int(input("Enter an integer")))
Also, as sheepez mentioned below, your outer brackets are redundant. You can simply use
num = int(input("Enter an integer"))

Why does this loop only once? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
while count > 0:
if count = 0:
return n
elif count < 0:
print(" ") # prints empty if n is below 0
else:
count = count - 1
collect += math.ceil((n - 5)/2)
return collect
The inputs are (1003, 3) - result is 499, which means it just loop once and subtracts 5 and then divides by 2, then it stops. Anyone know why?
Your inner conditionals don't make sense with the while. And you have a return statement in the loop, so yes, it only looped once.
Start with this
import math
n, count = (1003, 3)
print("N = " + str(n))
while count > 0:
n = math.ceil((n - 5) / 2) # Update this to do whatever your logic is
print(count, n)
count -= 1
if n < 0:
print("N is negative")
else:
print("N = " + str(n))
You have several problems.
First, your syntax isn't indented evenly.
Second, your if statement have = instead of ==. The first one for assigning values to variables, the second is for checking equality.
Third, you have a return statement that's going to exit from whatever function this loop is inside.

Python Heads and Tails [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I need help for a Python game I am currently creating. When running the code it will stay open and do nothing after entering a number of times to flip. Here is what I have so far:
# Heads and Tails generator
# User how many times they wish to flip a coin and will recieve the results
CoinTosses = int(input("How many coins do you wish to flip: "))
Heads = 0
Tails = 0
CurrentCoinToss = 0
from random import randint
while CoinTosses != 0:
CurrentCoinToss == int(randint(1, 2))
if CurrentCoinToss == 1:
Heads += 1
CoinTosses -= 1
if CurrentCoinToss == 2:
Tails += 1
CoinTosses -= 1
print("During this round you recieved: ", Heads, " and", Tails, " Tails!")
input("Press the enter key to exit")
What is wrong with this? I have studied my code and nothing SHOULD be wrong.
Change this line
CurrentCoinToss == int(randint(1, 2))
to this
CurrentCoinToss = int(randint(1, 2))
In the while loop you've written:
CurrentCoinToss == int(randint(1, 2))
Which actually tests the value of CurrentCoinToss, but doesn't give it a value.
Change it by:
CurrentCoinToss = int(randint(1, 2))

Loop being ignored by Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I'm a beginner to python, and when trying to see if I could make a simple program myself, I ran into this problem:
class y:
def out(self):
print("restarting")
choice = y
choice.out
while choice == y: # loop until user stops
while j >= 0: # loop until j < 0
print('lives:', j)
j = j - 1
print('out of lives!')
print('restart?')
choice = input(' Y or N ') # Ask user to restart or not
Everything works once, but Python seems to ignore the first loop (while choice == y). Have I forgotten a step, or am I doing this wrong altogether?
I don't think you need a class y here. If you just want to loop until choice isn't the character "y", then you can use ordinary strings.
choice = "y"
while choice == "y": # loop until user stops
j = 3
while j >= 0: # loop until j < 0
print('lives:', j)
j = j - 1
print('out of lives!')
print('restart?')
choice = input(' Y or N ') # Ask user to restart or not
Result:
lives: 3
lives: 2
lives: 1
lives: 0
out of lives!
restart?
Y or N y
lives: 3
lives: 2
lives: 1
lives: 0
out of lives!
restart?
Y or N n
The program loops until the user enters a value other than "y".

Categories