Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I'm learning Python and can't work out why the following doesn't work.
Can anyone advise? code below
thanks
# Make sure that the_flying_circus() returns True
print "What is your number?"
num = input()
print "What is bla?"
bla = input()
def the_flying_circus():
if num ==4 and bla=="bla": # Start coding here!
return True
print "Well done!"
# Don't forget to indent
# the code inside this block!
elif num == 2 or bla== zog:
print "OK"
# Keep going here.
# You'll want to add the else statement, too!
else:
print "Bad luck!"
the_flying_circus()
The return True is probably not what you want to have on the top of the if block. Try removing it.
The only condition that will return True is num==4 and bla=='bla'. Otherwise, the return value is None. However, 'Well done!' will never be printed since the return statement occurs first.
Couple of things...
1) return True should be moved to the end of the function (as mentioned by others)
2) watch how you collect input... use raw_input for your string, use input for the number.
This works for me:
def the_flying_circus():
if a==4 and b=='bla':
print "Well done!"
elif a==2 or b=="zog":
print "OK"
else:
print "Bad luck!"
return 1
a = input("What is your number? ")
b = raw_input("What is bla? ")
the_flying_circus()
Related
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 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")
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.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
I would like to know how I could allow my code to let the user restart the quiz at any time throughout the program however, I have no idea how to do this from scratch. It preferably needs to be fairly simple. Would it be easiest doing it as an if statement and if so what would I put in it? I tried this:
while True:
# main program
while True:
answer = raw_input('Run again? (y/n): ')
if answer in ('y', 'n'):
break
print 'Invalid input.'
if answer == 'y':
continue
else:
print 'Goodbye'
break
breaking your problem down into parts is the first step
# 1. ask a question and validate the response
def ask_question(prompt,possible_choices):
while True:
result = raw_input(prompt)
if result in possible_choices:
return result
print "Invalid Response Please Enter One Of:",possible_choices
# 2. run a whole quiz (ask all our questions)
def do_quiz():
answers = []
for question_text in questions_list:
answer = ask_question(question_text,['a','b','c','d','quit','restart')
if answer == "restart":
return False
# instead of returning false we could also simply call recursively
# return do_quiz()
elif answer == "quit":
print "Adios muchacho"
sys.exit(0)
else:
answers.append(answer)
return answers
# 3. play forever (or atleast until the user wants to quit...)
while True:
results = do_quiz()
if not results:
print "Lets Start Over!!"
continue
else:
check_answers(results)
if raw_input("Play again?")[0].lower() == "n":
print "Goodbye!"
sys.exit(1)
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
RANDOM_COR=random.randrange(5,6)
def check_xy_data():
global COUNT
COUNT=0
input_xy=input("input(x,y) : ")
think_xy=list(map(int,input_xy.split(",")))
if(random_array[think_xy[0]][think_xy[1]] == "C"):
screen_array[think_xy[0]][think_xy[1]] = "O"
COUNT=COUNT+1
else:
screen_array[think_xy[0]][think_xy[1]] = "X"
def main():
make_intro()
init_screen_array ()
init_random_array ()
make_random_num(RANDOM_COR)
while(True):
check_xy_data()
draw_outline_start(TOTAL_COL_NUM//2)
draw_out_rowline(TOTAL_COL_NUM//2, "Input : ")
draw_out_rowline(TOTAL_COL_NUM//2, "Correct : ")
draw_out_rowline(TOTAL_COL_NUM//2, "Error : ")
draw_out_rowline(TOTAL_COL_NUM//2, "Total : ")
draw_outline_mid(TOTAL_COL_NUM//2)
if(COUNT==RANDOM_COR-1):
break
The if at the bottom of my code is supposed to get me out of the while loop, but I'm stuck in an infinite loop. Help?
(assignment, 2016) 예고편 The Assignment | 어싸인먼트 감독: 월터 힐 각본: 월터 힐, 데니스 해밀 출연: 김성훈 출연 현빈, 유해진, 김주혁 개봉 2016 한국 상세보기 그간...
Try this change:
RANDOM_COR=random.randrange(5,6)
COUNT = 0
def check_xy_data():
global COUNT
With COUNT inside check_xy_data, you set it back to 0 on every call. It can never reach more than 1. Your check is whether it's in the range 5-6. This is never true, so you can never leave the loop.
Note that trivial debugging skills would have found this: just stick a print statement before you test your loop condition, to see what the values are. Use that next time ... :-)