Hi I'm new to programming, and ran into some problems while practicing using python.
So basically my task is to create a simple quiz with (t/f) as the answer. So here's my code:
def quiz(question,ans):
newpara = input(question)
if newpara == ans:
print("correct")
else:
print("incorrect")
return(newpara)
quiz_eval = quiz("You should save your notebook: ","t")
print("your answer is ",quiz_eval)
When the user input something, this code prints:
your answer is hi
However, I want it to print "your answer is correct/incorrect".
I'm not sure what has gone wrong here. Any thoughts?
Then you should be returning either "correct" or "incorrect". You function returns the value of the input so it's normal to get that output. Try this:
def quiz(question,ans):
newpara = input(question)
if newpara == ans:
answer= "correct"
else:
answer= "incorrect"
return(answer)
quiz_eval = quiz("You should save your notebook: ","t")
print("your answer is ",quiz_eval)
Note that my answer is just to answer your question and give the output the way you want it. If your program requires to limit the input of the user to either "t" or "f", then you need to add more conditions in your code.
Related
I'm really new in Coding, with Python.
I was trying to make a Vocabulary exercise program for a Language that i am learning it right now. So the concept is, if the word "abhängen" is shown at the Console, i have to write "von" which is the right word to come after that word, which is "abhängen". And the program will show if its right or wrong, and loops the input to get the right answer.
But since there are tons of vocabulary, i have to make same loop over and over again just by using while True and changing a,b,c for the variables and the word between "". Is there a way to make it shorter maybe by using list or something?
And if its possible, can i somehow make the order of the questions randomly? Since this code always shows the first question as abhängen and second as abrechnen.
Sorry if this was some kind of dumb question to ask, have nowhere to ask haha
have a nice day guys
while True:
a = input("abhängen ")
if a == "von":
print("You're right")
break
else:
print("Wrong")
while True:
c = input("abrechnen ")
if c == "mit":
print("You're right")
break
else:
print("Wrong")
It looks like a great example to learn usage of lists in Python.
You can use list of tuples and random module for this task. See random.shuffle documentation here
words_and_answers = [("abhängen ", "von"), ("abrechnen ", "mit")]
random.shuffle(words_and_answers)
for input_word, correct_answer in words_and_answers:
while True:
user_response = input(input_word)
if user_response == correct_answer:
print("You're right")
break
else:
print("Wrong")
I think a possible solution is to use a dictionary in which the keys are the words "abhängen", "abrechnen", ecc... And the values are the correct words that come after. So you can write a thing like this:
vocabulary = {
"abhängen" : "von",
"abrechnen" : "mit"
}
for i in vocabulary:
a = input(i + " ")
if a == vocabulary[i]:
print("You are right")
else:
print("Wrong")
Note that in this case the loop is not infinite! This method allows you to have more than one word correct for each key
For the randomly access to the keys, you can make a list with the keys and use the random.shuffle() method, like this:
import random
keys = list(vocabulary.keys())
random.shuffle(keys)
And then your loop will be:
for i in keys:
...
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
I want the code to say one thing if you type in good, and another if you type in bad. Please don't bully me this is my first hour of coding lol. Its putting out both text strings no matter what I answer, good, bad, my name, any text
name = input("Enter your name please: ")
print("hi " + name + " how are you doing?")
Answer = input("Good or bad?")
if Answer : "Good"
print("That's great to hear!")
if Answer : "Bad"
print("That sucks, why?")
welcome to the world of coding. You are using the if else syntax wrong. It looks like
if condition:
Some code
else if condition:
Some code
else
Some code
How a condition looks like is
firstValue comparing operator(==(Equals),!=(Not equals),>,<,>=,=< and more) second value
So in your case,
name = input("Enter your name please: ")
print("hi " + name + " how are you doing?")
Answer = input("Good or bad?")
if Answer == "Good":# Answer is the first value, == is the operator, "Good" is the second operator
print("That's great to hear!")
if Answer == "Bad" :# Answer is the first value, == is the operator, "Bad" is the second operator
print("That sucks, why?")
I hope you understand this. Ways you can make improvements (Don't pay attention if you do not understand)\
1> If we convert answer into lowercase, the user can write Good, gOod, good etc. and you will always be able to check it. You do this by using the lower(), method. Call it like this
lower(String)
For your code
name = input("Enter your name please: ")
print("hi " + name + " how are you doing?")
Answer = lower(input("Good or bad?"))# Convert into lowercase
if Answer == "good":
print("That's great to hear!")
if Answer == "bad" :
print("That sucks, why?")
2> Ok this may seem very complicated, but if you understand it, its very very helpful. What we do is keep asking the user the question till they give the correct answer. It will help us deal with the situation where user writes something we do not want
How we do this is by running a while loop. What it does is keep running until a certain condition becomes false. There are 2 ways we can use it for this program.
WAY 1
name = input("Enter your name please: ")
repeat = True # Defining a condition
print("hi " + name + " how are you doing?")
Answer = lower(input("Good or bad?"))# Convert into lowercase
while repeat: #Saying to the code to run till we have a good result
if Answer == "good":
print("That's great to hear!")
repeat = False #Doing this will make the condition false, which causes us to break this loop and move out
if Answer == "bad" :
print("That sucks, why?")
repeat = False #Doing this will make the condition false, which causes us to break this loop and move out
Answer = input("Please give valid input. Good or Bad")
#Add your remaining code here. please check the indentations
WAY 2
In this, we dont define a new repeat variable. We use a special command called break
name = input("Enter your name please: ")
print("hi " + name + " how are you doing?")
Answer = lower(input("Good or bad?"))# Convert into lowercase
while True:# Making a loop which is always true
if Answer == "good":
print("That's great to hear!")
break #Breaking out of the loop
if Answer == "bad" :
print("That sucks, why?")
break #Breaking out of the loop
Answer = lower(input("Please give valid input. Good or Bad"))
#Add your code after this
You can try below code too, it is using python def, please note indentation plays very important role python and if code is not indented properly it won't behave properly. You can read more about Python indentation here. I would recommend to look at W3schools python tutorials, they not only provide lots of examples but also provide interactive shell where you can try out the code.
def greeting(greet):
if greet == "Good":
print("That's great to hear!")
if greet == "Bad":
print("That sucks, why?")
name = input("Enter your name please: ")
print("hi " + name + " how are you doing?")
Answer = input("Good or bad?")
greeting(Answer)
I'm currently working on a guessing game assignment. The assignment uses a dictionary to store the course name which is the key and the course number which is the value. The user guesses the course number of the course name given. If the value matches the key then it should print "correct!" and vice versa.
I have gotten the program to display the keys one at a time with an input statement separating them. I've gotten the correct/incorrect counters working. I'm not able to get an if statement working which is supposed to check if the value matches the key. It prints incorrect every time regardless of if the answer is correct. I realize there's probably something wrong with the condition of the if statement because i'm not really sure how to extract one value at a time.
Here's what I have so far:
# Mainline
def main():
programming_courses={"Computer Concepts":"IT 1025",\
"Programming Logic":"IT 1050",\
"Java Programming":"IT 2670",\
"C++ Programming":"IT 2650",\
"Python Programming":"IT 2800"}
print ("Learn your programming courses!\n")
correct=0
incorrect=0
v=0
# Game Loop
for key in programming_courses.keys():
print(key)
answer = input("Enter the Course Number: ")
if answer != programming_courses.values():
print("Incorrect")
incorrect += 1
else:
print("Correct!")
correct += 1
# Display correct and incorrect answers
print ("You missed ",incorrect," courses.")
print ("You got ",correct," courses.\n")
# Entry Point
response=""
while (response!="n"):
main()
response=input("\n\nPlay again?(y/n)\n# ")
Your problem is here:
if answer != programming_courses.values():
programming_courses.values() is a list of all the values in the dictionary. If you don't understand what's happening in your program, it's really helpful to just print stuff out and see if it looks like what you expect.
What you want is the specific value for the key you're on right now, which you need to look up from the dictionary like so:
if answer != programming_courses[key]:
Also, iterating over a dict gives you the keys by default, so you can just say:
for key in programming_courses:
You don't need to use .keys() there.
Your problem is when you are checking your dict. Currently your code is comparing the answer to a list of all the values in the dict:
out[]:
dict_values(['IT 1025', 'IT 1050', 'IT 2670', 'IT 2650', 'IT 2800'])
If you change to the following it works, by taking the specific value from the dict with the given key:
for key in programming_courses.keys():
print(key)
answer = input("Enter the Course Number: ")
if answer != programming_courses[key]:
print("Incorrect")
incorrect += 1
else:
print("Correct!")
correct += 1
you could try this
if answer != programming_courses[key]:
I am new to python and trying to learn by doing small projects.
I am trying to write a program that displays the names of the four properties and
asks the user to identify the property that is not a railroad. The user should be informed if the selection is correct or not.
properties = "Reading,","Pennsylvania","B & O","Short Line"
question = str(input("Which is not a railroad?")) **Short Line**
if properties == "Short Line":
print("correct")
else:
print("incorrect")
However, my final output shows as "incorrect", what am i doing wrong?
The four railroad properties
are Reading, Pennsylvania,
B & O, and Short Line.
Which is not a railroad? Short Line
Correct.
Short Line is a bus company.
Couple things I see with this code you have posted.
First, not sure if you actually have **Short Line** in your actual code but if you are trying to comment use # That way it won't be interpreted at run time.
Second as mentioned in other answers you are checking against properties which is pulling in your array. You should be checking against your input which is stored at question.
properties = "Reading,","Pennsylvania","B & O","Short Line"
question = str(input("Which is not a railroad?")) # **Short Line**
if question == "Short Line": # replaced properties with question
print("correct")
else:
print("incorrect")
print(properties)
print(question)
I find that when I am having troubles understanding why something is not working I throw some print statements in to see what the variables are doing.
You may want to catch the user in a loop, otherwise you would have to constantly have to run the code to find the correct answer(unless that is the desired behavior, then you can leave it as you have it). Also, be aware that you may want to uppercase or lowercase because a user may provide the answer as "Short line" (lower case "L"), and the code will return as incorrect. Of course, that depends on what you will accept as an answer.
Sample
print ("Reading,Pennsylvania,B & O, or Short Line. Which is not a railroad?")
user_input = input("Please provide an answer: ")
# != the loop will close once the user inputs short line in any form
# The upper.() will convert a user_input string to all caps
while user_input.upper() != "SHORT LINE":
print ("Incorrect, Please try again.")
user_input = input("Which one is not a railroad? ")
print ("Correct")
Prettied it up for you
print( "Reading, Pennsylvania, B & O, and Short Line. Which is not a railroad?" )
print("Which is not a railroad?")
answer = input()
if answer == "Short Line":
print("correct")
else:
print("incorrect")
I'm trying to create a simple script that will will ask a question to which the user will input an answer (Or a prompt with selectable answers could appear?), and the program would output a response based on the input.
For example, if I were to say
prompt1=input('Can I make this stupid thing work?')
I would have something along the lines of
if prompt1='yes':
print('Hooray, I can!')
else prompt1='No':
print('Well I did anyway!')
elif prompt1=#an answer that wouldn't be yes or no
#repeat prompt1
I'm probably going about this the wrong way. Please be as descriptive as possible as this is a learning exercise for me. Thanks in advance!
You are pretty close. Read a good tutorial :)
#!python3
while True:
prompt1=input('Can I make this stupid thing work?').lower()
if prompt1 == 'yes':
print('Hooray, I can!')
elif prompt1 == 'no':
print('Well I did anyway!')
else:
print('Huh?') #an answer that wouldn't be yes or no
while True will loop the program forever.
Use == to test for equality.
Use .lower() to make it easier to test for answers regardless of case.
if/elif/elif/.../else is the correct sequence for testing.
Here's a Python 2 version:
#!python2
while True:
prompt1=raw_input('Can I make this stupid thing work?').lower()
if prompt1 == 'yes':
print 'Hooray, I can!'
elif prompt1 == 'no':
print 'Well I did anyway!'
else:
print 'Huh?' #an answer that wouldn't be yes or no
raw_input is used instead of input. input in Python 2 will tries to interpret the input as Python code.
print is a statement instead of a function. Don't use () with it.
Another example, this time as a function.
def prompt1():
answer = raw_input("Can I make this stupid thing work?").lower()
if answer == 'yes' or answer == 'y':
print "Hooray, I can!"
elif answer == 'no' or answer == 'n':
print "Well I did anyway!"
else:
print "You didn't pick yes or no, try again."
prompt1()
prompt1()