How to fix a while syntax error in python? [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 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"))

Related

My "test if this is a prime number" code crashes when I test numbers greater than 4 [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 month.
Improve this question
I'm a Python noobie, please have mercy.
I tried to create a code to test if a given number is a prime number. I tried the %, sqrt, and n methods. All ended with the same result: when I try to test the code for numbers greater than 4, the code returns NULL and says "Process finished with exit code 0"
Can you please help me figure out what I'm doing wrong?
Here's the code I wrote (this is the 'n' method where I apply % to numbers all the way up to n; I know it's not the most efficient, but I'll fix that after I make sure it works):
x = 5
if x > 1:
for i in range(2, x):
if (x % i) == 0:
print("NOT prime")
break
else:
print("YES, it's prime")
else:
print("NOT prime, because the number is < 1")
break should be indented once more, so it does not break the loop when it is not a prime.

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

It shows the error as 'fact' not defined.why? [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 2 years ago.
Improve this question
def factorial(n):
if(n == 1):
fact = 1
return fact
else:
fact = a * factorial(n - 1)
return fact
a = int(input("Enter a number to find its factorial: ")
if (a > 0):
print("The factorial of", a, "is:", fact)
else:
print("Enter a positive value to find its factorial")
In the above code it tells me that - NameError name 'fact' is not defined .
Lines starting from a = int... should be outside your function. Once that is done all you need is to add fact = factorial(a).
Find the correct logic below.
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
# check if the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))

If/Elif Python Statement [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 2 years ago.
Improve this question
New to this. My autochecker says that line 3 is incorrect.
score = input("Enter Score: ")
s = float(score)
if s >= 0.9
("A")
elif s >= 0.8
print ("B")
elif s >=0.7
print ("C")
elif s >=0.6
print ("D")
elif s < 0.5
print ("F")
else
print ("Enter number")
You need a colon after each if/elif/else line.
You need to add print before "A".

Using random in an if statement [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 5 years ago.
Improve this question
I am trying to make a random number guessing game but I cant get the if statement to check if the users input is = to the random number
import random
realNumber = random.randint(1, 50)
print(realNumber)
myNumber = print(input("Guess the number from 1 to 50: "))
if int(myNumber) == realNumber:
print("You win")
else:
print("Nope guess again")
The unintended behavior of your program is due to this line:
myNumber = print(input("Guess the number from 1 to 50: "))
Here, you are trying to assign myNumber to the return value of the print statement (Which is None) and not the value obtained from the input() statement. To fix this, simply remove the print() around the input.
myNumber = input("Guess the number from 1 to 50: ")
Hope this helped!
You don't need the print statement around input.
import random
realNumber = random.randint(1, 50)
print(realNumber)
myNumber = input("Guess the number from 1 to 50: ")
if int(myNumber) == realNumber:
print("You win")
else:
print("Nope guess again")
Note that this code will not work if the user enters something besides an integer, because the int() call will not cast correctly

Categories