NameError: name 'grade' is not defined [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 am trying to write a program that will number grade output when the user enters a letter grade. I am getting this error: NameError: name 'grade' is not defined. Can someone tell me what this error means and help me figure out how to fix it. I am new to programming so I am realy lost.
letterGrade = (input("Please enter a letter grade: "))
if grade >= A:
grade == "4.0"
elif grade < A:
grade < "4.0"
print(grade)

It looks like your if statements are checking for the variable grade, but your input is being assigned to letterGrade. You can fix this either by changing your first line to
grade = input("Please enter a letter grade: ")
or change every instance of grade to letterGrade.
Additionally, grade == "4.0" should probably be grade = "4.0"; x == y checks if x is equal to y, whereas x = y sets x equal to y.

Related

int(intput()) inside a function does not work? [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 1 year ago.
Improve this question
when I try to use the following function:
def ea():
x,y = int(input("enter age:")),input("enter name:")
if x > 0:
print(f"My name is {y} and I am {365*(x)} on earth")
else:
ea()
I get this error:
TypeError: int() takes 0 positional arguments but 1 was given.
I don't understand why as int(input()) works when not used in a function. Can anyone help?
Thanks in advance
def ea():
x, y = input("enter your name and age: ").split(" ")
y = int(y)
if y > 0:
print(f"My name is {x} and I am {365*(y)} on earth")
else:
ea()
Output :
>>> ea()
enter your name and age: thor 18
My name is thor and I am 6570 on earth
You Can Try This
def ea():
x = int(input("enter age:"))
y = input("enter name:")
if x > 0:
print(f"My name is {y} and I am {365*(x)} on earth")
else:
ea()

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".

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"))

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

This python code calls a function that returns a value but user input is asked twice [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 7 years ago.
Improve this question
I am trying to run this code but its asking me input two times.I just want the function "grades" to be called and which will return value according to user input. But when I execute this code,it asks for user input two times.
def grades(score):
try:
score = float(input("enter the score please"))
except:
score = -1
if score<0.0 or score>1.0:
return "Wrong score"
elif score==0.9:
return "A"
elif score==0.8:
return "B"
elif score==0.7:
return "C"
elif score==0.6:
return "D"
elif score==0.5:
return "B"
else :
return "F"
MyScore=float(input("enter my score"))
result=grades(MyScore)
print(result)
I want the function "grades" to be called but the user input should be asked only once.
Within the function grades you're asking for the score:
score = float(input("enter the score please"))
and then outside of the function, you're also doing it:
MyScore=float(input("enter my score"))
Remove one of the two input statements, and it will only ask you the one time :)
def grades(score):
if score<0.0 or score>1.0:
return "Wrong score"
elif score==0.9:
return "A"
elif score==0.8:
return "B"
elif score==0.7:
return "C"
elif score==0.6:
return "D"
elif score==0.5:
return "B"
else :
return "F"
try:
score = float(input("enter the score please"))
except:
score = -1
result=grades(score)
print(result)'

Categories