A game to go on an adventure in python interactive mode

A game to go on an adventure in python interactive mode

A game until you enter a quest-like adventure in python's interactive mode. It helped me to understand the basics of python because it holds the basic syntax of python.


▼ Game content (with RPG-style BGM) https://youtu.be/SPq_8Vc43bI

I couldn't put the gif video, so I added music to the video and uploaded it to youtube.


## Syntax and method used

** ・ input method ** Can be entered by the user at the console. Store the entered value in a variable.

** ・ if statement ** Conditional branch

** ・ while statement ** Loop until "yes" is entered

** ・ Dictionary type array ** Variable = {'key 1': value 1,'key 2': value 2, ...} Save the value with a name (key). Since the order is random, specify the key when extracting.

** ・ for statement ** Used to retrieve the contents of an array one by one. If the array is dictionary type, the keys will be extracted one by one.

** · try except syntax ** Describes the processing when an error occurs. except KeyError: An error that occurs when you enter a key that does not exist in the dictionary array.

** ・ print function ** Output a string to the console. Connect variables and strings with +. Convert the numerical value to a character string (str).

** ・ str function ** Convert a number to a string.

** ・ format function ** One of variable expansion. Print variables in the print function. You can use either the method of specifying the values in the order of the array number or the method of specifying the values with a name.

** ・ f character string ** One of variable expansion. Print variables in the print function. Easy to write and easy to use.


## code
start = input('Make an adventure (yes or no):')

if start=='Yes':
    start=1
else:
    start=0

while start==0:
    start = input('Make an adventure (yes or no):')
    
    if start=='Yes':
        start=1
    else:
        start=0

print('--------------------------------------------------\n')
name = input('Please enter your name:')

print('--------------------------------------------------\n')
print('Good morning'+name+'.. It's already Asa.\n')
print('Today is very important.\n'+name+'But for the first time, it was a fold.\n')
print('First of all, prepare the sobi.')

print('--------------------------------------------------\n')
start = input('Look at Genzai's Shojikin (yes or no):')

if start=='Yes':
    start=1
else:
    start=0

while start==0:
    start = input('Look at Genzai's Shojikin (yes or no):')
    
    if start=='Yes':
        start=1
    else:
        start=0


money=5000
print('--------------------------------------------------\n')
print('Genzai no Shojikin'+str(money)+'It's gold.\n')    

sords={'Hinoki cypress':300, 'Seido sword':1200, 'Hagane sword':2000, 'Yusha's sword':4600}
shields={'Pan lid':400, 'Mahou no Tate':1480, 'Fire':1800, 'Yusha no Tate':4600}
armors={'Kawa armor':500, 'Dangerous swimsuit':1370, 'Dragon mail':1910, 'Yusha armor':4600}
helmets={'Kawanoboshi':520, 'Usamimi Band':1420, 'Happiness':1840, 'Yusha's helmet':4600}


buysord="None"
buyshield="None"
buyarmor="None"
buyhelmet="None"


#Weapon purchase
print('--------------------------------------------------\n')
start = input('Look at the weapon (yes or no):')
if start=='Yes':
    for sord in sords:
        print('・'+sord+':'+str(sords[sord])+'gold')
            
    print('--------------------------------------------------\n')

    err=0
    while err==0:
        buysord = input('Which weapon do you want:')
        try:
            price=sords[buysord]
            err=1
        except KeyError:
            err=0

    if money >= price:
        print('\n'+str(price)+'Gold'+buysord+'I bought it.')

        money -= price
        print('Shojikin'+str(money)+'It's gold.')
    else: 
        print('\n I don't have enough.')
        print('I didn't have a weapon.\n')
        print('Shojikin'+str(money)+'It's gold.')


#Buy a shield
print('--------------------------------------------------\n')
start = input('Look at the vertical (yes or no):')
if start=='Yes':
    for shield in shields:
        print (f'・{shield}:{shields[shield]}gold')
            
    print('--------------------------------------------------\n')

    err=0
    while err==0:
        buyshield = input('Which one do you want?')
        try:
            price=shields[buyshield]
            err=1
        except KeyError:
            err=0

    if money >= price:
        print(f'\n{price}Gold{buyshield}I bought it.' )

        money -= price
        print(f'Shojikin{money}It's gold.')
    else: 
        print('\n I don't have enough.')
        print('It wasn't fresh.\n')
        print(f'Shojikin{money}It's gold.')


#Buy armor
print('--------------------------------------------------\n')
start = input('Look at the armor (yes or no):')
if start=='Yes':
    for armor in armors:
        print('・'+armor+':'+str(armors[armor])+'gold')
            
    print('--------------------------------------------------\n')

    err=0
    while err==0:
        buyarmor = input('Which armor do you want:')
        try:
            price=armors[buyarmor]
            err=1
        except KeyError:
            err=0

    if money >= price:
        print('\n{price}Gold{buyarmor}I bought it.'.format(price=price, buyarmor=buyarmor) )

        money -= price
        print('Shojikin{money}It's gold.'.format(money=money))
    else: 
        print('\n I don't have enough.')
        print('There was no armor.\n')
        print('Shojikin{money}It's gold.'.format(money=money))



#Purchasing a helmet
print('--------------------------------------------------\n')
start = input('Look at the helmet (yes or no):')
if start=='Yes':
    for helmet in helmets:
        print(f'・{helmet}:{helmets[helmet]}gold')
            
    print('--------------------------------------------------\n')

    err=0
    while err==0:
        buyhelmet = input('Which helmet do you wear:')
        try:
            price=helmets[buyhelmet]
            err=1
        except KeyError:
            err=0

    if money >= price:
        print('\n{}Gold{}I bought it.'.format(price, buyhelmet) )

        money -= price
        print('Shojikin{kane}It's gold.'.format(kane=money))
    else: 
        print('\n I don't have enough.')
        print('There was no armor.\n')


print('--------------------------------------------------\n')
print('Sobi no Konyu is over.\n\n')

print('★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★')
print(f'\n<{name}Sobi>')
print(f'・ Weapon:{buysord}')
print(f'・ Vertical:{buyshield}')
print(f'・ Armor:{buyarmor}')
print(f'・ Kabuto:{buyhelmet}\n')
print('★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★')


print('\n\n Please do your best.\n')
print('end\n\n')

It's a long time, but the following is repeated after purchasing a weapon. Use the format function or the f character string as the output method of the print function.

Recommended Posts

A game to go on an adventure in python interactive mode
An alternative to `pause` in Python
How to make a string into an array or an array into a string in Python
How to create a heatmap with an arbitrary domain in Python
I tried to implement a misunderstood prisoner's dilemma game in Python
How to get a stacktrace in python
Create a shortcut to run a Python file in VScode on your terminal
[Python] Create a linebot to write a name and age on an image
I want to color a part of an Excel string in Python
A memorandum because I stumbled on trying to use MeCab in Python
I tried to implement a card game of playing cards in Python
Try to calculate a statistical problem in Python
How to clear tuples in a list (Python)
To execute a Python enumerate function in JavaScript
How to embed a variable in a python string
[Python] Make a game with Pyxel-Use an editor-
I want to create a window in Python
I tried playing a typing game in Python
How to create a JSON file in Python
I want to make a game with Python
How to get help in an interactive shell
A note on optimizing blackbox functions in Python
A clever way to time processing in Python
Steps to develop a web application in Python
Make a rock-paper-scissors game in one line (python)
To add a module to python put in Julialang
How to notify a Discord channel in Python
To write to Error Repoting in Python on GAE
[Python] How to draw a histogram in Matplotlib
Insert an object inside a string in Python
Try Python interactive mode inside a Docker container
Use dHash to locate on the course from a scene in a racing game
A complete guidebook to using pyenv, pip and python in an offline environment
Searching for an efficient way to write a Dockerfile in Python with poetry
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
Try to make a Python module in C language
Build an interactive environment for machine learning in Python
Created a Python library to write complex comprehensions and reduce in an easy-to-read manner
I want to write in Python! (2) Let's write a test
Until drawing a 3D graph in Python on windows10
Make a simple Slackbot with interactive button in python
[Python] How to expand variables in a character string
Create a plugin to run Python Doctest in Vim (2)
I tried to implement a pseudo pachislot in Python
A memorandum on how to use keras.preprocessing.image in Keras
Create a plugin to run Python Doctest in Vim (1)
A memorandum to run a python script in a bat file
How to use python interactive mode with git bash
I want to randomly sample a file in Python
How to build a Django (python) environment on docker
I want to work with a robot in python.
Try running a Schedule to start and stop an instance on AWS Lambda (Python)
Things to note when initializing a list in Python
How to make an interactive CLI tool in Golang
A note on handling variables in Python recursive functions
How to create an image uploader in Bottle (Python)
How to deploy a Go application to an ECS instance
Write a log-scale histogram on the x-axis in python