This time, I will write a program of gold ax and silver ax, which also serves as a review of the past.
Please watch the video if you like.
First, put input () in a variable called answer.
You can display something like a wording when you ask for input by writing something in ().
Next, enter the condition with if.
Use if when writing the first condition.
Here, if what is in the answer is "gold" in letters, it is instructed to return "really?".
Use elif when the second and subsequent conditions are required in addition to the condition issued by if.
Unlike the case of if, there is no problem if there are several.
Use else to process if and elif that do not correspond to any of them uniformly.
I will actually move it.
answer = input("Did you drop the gold ax? Is it a silver ax?")
if answer == "Money":
print("Really?")
elif answer == "Silver":
print("Are you lying?")
else:
print("You are honest!")
Did you drop the gold ax? Is it a silver ax? Money
Really?
Did you drop the gold ax? Is it a silver ax? Silver
Are you lying?
Did you drop the gold ax? Is it a silver ax? Neither
You are honest!
In this way, for inputs other than "gold" and "silver", the movement specified by else is performed.
input () can also be done with numbers.
In that case, it is written as int (input ()).
If you do not make a mistake, such as setting the condition to a number, it will work in the same way as the previous one.
I will actually move this as well.
answer = int(input("Did you drop the gold ax? Is it a silver ax? Gold is 1,Silver is 2"))
if answer == 1:
print("Really?")
elif answer == 2:
print("Are you lying?")
else:
print("You are honest!")
Did you drop the gold ax? Is it a silver ax? Gold is 1,Silver is 21
Really?
Did you drop the gold ax? Is it a silver ax? Gold is 1,22 for silver
Are you lying?
Did you drop the gold ax? Is it a silver ax? Gold is 1,Silver is 26
You are honest!
Did you drop the gold ax? Is it a silver ax? Gold is 1,Silver is neither 2
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-f1f05ecb0eea> in <module>
----> 1 answer = int(input("Did you drop the gold ax? Is it a silver ax? Gold is 1,Silver is 2"))
2 if answer == 1:
3 print("Really?")
4 elif answer == 2:
5 print("Are you lying?")
ValueError: invalid literal for int() with base 10: 'Neither'
It works just like letters for numbers.
If you enter a character such as "Neither" while specifying a number, an error will occur.
Click here for a list of each story.
Recommended Posts