Mayungo's Python Learning Episode 9: Gold Ax and Silver Ax

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

Mayungo's Python Learning Episode 9: Gold Ax and Silver Ax
Mayungo's Python Learning Episode 8: I tried input
Mayungo's Python Learning Note: List of stories and links
Mayungo's Python Learning Episode 1: I tried printing with print
Mayungo's Python Learning Episode 3: I tried to print numbers with print
Mayungo's Python Learning Episode 7: I tried printing with if, elif, else
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
Mayungo's Python Learning Episode 2: I tried to put out characters with variables
Python data structure and operation (Python learning memo ③)
python learning
Python and machine learning environment construction (macOS)
Mayungo's Python Learning Episode 5: I tried to do four arithmetic operations with numbers
Python learning notes
python learning output
Python learning site
Python learning day 4
Python learning (supplement)
Deep learning × Python
python learning notes
Python learning memo for machine learning by Chainer Chapters 1 and 2
Set up python and machine learning libraries on Ubuntu