This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
I am making a small quiz that has different difficulties, when doing my easy section my if statement always prints incorrect even with the correct answer - i am still new to python it might be a very easy thing to solve
How can I know if this is a duplicate if i don't know what is wrong
here is my code
def easy():
score = 0
print("Welcome to the easy section!")
print("****************************")
print("Random Access Memory(1)")
print("Radical Amazonian Mandem(2)")
qe1 = input("What does RAM stand for?: ")
if qe1 == 1:
score = score + 1
print("Well done, next question:")
if qe1 != 1:
print("Incorrect, next question:")
else:
easy()
input function return a string, you should cast it to int:
qe1 = int(input("What does RAM stand for?: "))
Related
This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 6 months ago.
I'm trying to create a simple program using function and if else. Even though there is no errors, my program doesn't give expected result
value1 = float(input("Enter your first number:"))
value2 = float(input("Enter your second number:"))
question= input("choose mode:" )
add = True
def nor_cal(num1, num2):
if question == add:
total = num1+num2
print("the total is: ")
print(total)
else:
print("Invalid operator")
result1 = nor_cal(value1, value2)
print(result1)
when i run the program, it shows like this:
Enter your first number:2
Enter your second number:3
choose mode:add
Invalid operator
None
I don't know where i'm wrong please help !
The bug is in the line if question == add:, what you're asking the program to do is to compare the variable question (which is "add") to the variable add (which is True).
You're going to want to use if question == "add": instead.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
Helloo
I'm trying to make a simple game where you have to choose either 1 or 2 and one of them is correct. I've used a simple random generator to choose either 1 or 2 as the correct answer.
def guess():
print("")
print("Select a number, 1 or 2")
print("")
from random import randint
ran = randint(1, 2)
nmr = input("")
if nmr == ran:
print("That's correct!")
else:
print("Wrong number")
Every time I answer it prints "Wrong number".
I've also tried printing the random number before answering but it still takes it as incorrect. Any idea what is wrong there?
The problem is that you are comparing a string with an int. That always gives False.
ran = randint(1, 2) #int
nmr = input("") #str
So for it to work, either convert ran to str or nmr to int
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
I am using the newest python software and I am really stumped on printing a certain line in the code.
name = input("Before we begin, what is your name? ")
print("Cool, nice to meet you "+name+"!")
score = 0
score = int(score)
count = 0
answer1 = "dogs"
answer01 = "dog"
question1 = ""
while question1 != answer1 and count < 2:
count = count + 1
question1 = input("What animal barks? ") #Asks the user a question
if question1.lower()== answer1:
print("Correct! Off to a great start, the right answer is dogs!")
score = score + 1
break
elif question1.lower()== answer01:
print("Correct! Off to a great start, the right answer is dogs!")
score = score + 1
break
elif count == 1:
print("That is wrong",name,"but you can try again. Hint: They like bones. ")
if count == 2 and question1 != answer1:
print("Incorrect again, sorry! The right answer was dogs.")
Please do correct me if that looks wrong, I am new to python. Anyway all I want to do is print the question again without repeating the introduction (start). My actual code is much longer and is an entire quiz and I need to repeat certain sections of code from different parts of the code. Please help! Plus I am sorry if the code didn't print properly when I pasted it into the my post.
So, the problem is that your questions are hardcoded, so they repeat etc. Instead, you could provide a data structure, storing all your questions, wrong ang right answers, which you would pass to some parsing logic. For example:
questions = ['What animal barks?', ...]
answers = [('dogs', 'dog'), ...]
hints = ['They like bones', ...]
def dialog(qst, ans, hnt):
score = 0
for question, answers, hint in zip(qst, ans, hnt):
incorrects = 0
while True:
usr_ans = input(question)
iscorrect = any(usr_ans.strip().casefold() == answ.strip().casefold() for answ in answers)
if incorrects < 5 or not iscorrect:
print('That is wrong, {}, but you can try again. Hint: {}'.format(name, hint))
incorrects += 1
elif incorrects >= 5:
print('Incorrect again, sorry! The right answer was {}.'.format(' or '.join(answers))
break
else:
print('Correct! Off to a great start, the right answer is {}!'.format(usr_ans))
score += 1
break
return score
It may look a bit complicated as you're starting to learn. I recommend not to code complete things, until the point in time, where you'd already known the basics. For a great start, try official Python Tutorial, and I'd recommend Lutz's Learning Python
This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 7 years ago.
this is all of my code so far. the function works if I input x manually. I'm also just starting out so any general info on how to generally make it more pythonic would be apreciated
def iseven(x):
if x % 2 == 0:
return x," is even"
else:
return x," is odd"
it = (1)
while it >= 0:
number = input("What number: ")
print iseven(number)
new_num = input("another number? answer (Y/N):")
if new_num == "Y":
it += 1
else:
it = 0
this is the error
NameError: name 'Y' is not defined
You may want to avoid input in such cases where you're processing general input and prefer raw_input:
https://docs.python.org/2/library/functions.html#raw_input
This avoid the eval that's taking place that's causing your error - it's looking in the program's scope for something named "Y". If you type "it", here, for example, it will evaluate the code, which is generally unsafe (though fairly benign here I suppose).
https://docs.python.org/2/library/functions.html#eval
Just change the line:
new_num = input("another number? answer (Y/N):")
to:
new_num = raw_input("another number? answer (Y/N):")
You need to change: while it >= 0: to while it: as well so that you can be able to exit the loop if you select N on prompt.
This question already has answers here:
How to create a loop in Python [duplicate]
(2 answers)
Closed 8 years ago.
I'm trying to write a while loop here is my code (homework so its basic):
import random
RandomNumber=(random.randint(0,100))
GuessedNumber=int(input("Guess any whole number between 0 and 100! "))
while RandomNumber != GuessedNumber:
if GuessedNumber==RandomNumber:
print("Well done you gessed correctly!")
else:
print("Unlucky guess again!")
If anyone knows what I'm doing wrong with my while loop, help would be appreciated; thanks.
You never update the value of GuessedNumber inside the loop. Therefore, if the code enters the loop, it will never leave it because RandomNumber != GuessedNumber will always be true.
You need to do something like this:
import random
RandomNumber=(random.randint(0,100))
GuessedNumber=int(input("Guess any whole number between 0 and 100! "))
while RandomNumber != GuessedNumber:
print("Unlucky guess again!")
GuessedNumber=int(input("Guess any whole number between 0 and 100! "))
print("Well done you gessed correctly!")
Notice how the value of GuessedNumber is now updated with each iteration of the loop.