DEV Community

JFG-0
JFG-0

Posted on

4 2

The first actual Python program I want to show

This is my final excersise from (Much appreciated) codecademy's CS101, a terminal game that applies a trivia in spanish, it creates a player and returns a score.

#create the trivia questions

import csv
from unicodedata import category
with open('D:/UX/01_Python_course/20220323_trivia_thor/preguntas_trivia.csv') as csv_file:
    csv_read = csv.reader(csv_file, delimiter= ',')
    trivia_list = list(csv_read)

marathon_list = []

def list_clean(list, new_list):
    for item in range(0, len(list)):
        list_to_work = trivia_list[item]
        check = 0 
        for i in range(0, len(list_to_work)):
            if list_to_work[i] != "":
                check += 1
            else:
                continue
        if check != 0: new_list.append(list[item])

list_clean(trivia_list, marathon_list)
#print(marathon_list)

#Here lives the right answer generator
import csv
from unicodedata import category
with open('D:/UX/01_Python_course/20220323_trivia_thor/preguntas_trivia.csv') as csv_file:
    csv_read = csv.reader(csv_file, delimiter= ',')
    trivia_list = list(csv_read)

correct_answers = dict()
for item in range(1, len(trivia_list)):
    correct_answers[str(trivia_list[item][0])] = str(trivia_list[item][-1])

#create a file that uses a class
class Scout:

    def __init__(self, name, age, achievements):
        self.name = name
        self.age = age
        self.achievements = achievements
        self.game_score = 0

    def __repr__(self) -> str: return "{name} es un caminante de Thor, tiene {age} años y su progresión actual es {achievement}".format(name = self.name, age = self.age, achievement = self.achievements)

    def update_score(self, attained_score):
        self.game_score = attained_score
        print('Muy bien {name_player}, tu puntaje de {score} se añadirá, gracias por participar!'.format(name_player = self.name, score = self.game_score))
#welcome

print("Bienvenido a la trivia de Thor, hoy conoceremos tu avance y nos dirás qué te parece el juego.\
    Ingresa tu nombre, quedará en minúsculas para efectos prácticos!")
nombre = input()
print("Hola " + nombre.lower())

#request age
edad = ""
status = False
print('ingresa tu edad en números!')

while status == False:
    ingresar_edad = input()
    try:
        if type(int(ingresar_edad)) == int:
            status = True 
    except:
        print("Tratemos de nuevo")
    finally:
            continue
print("Tu edad es " + str(ingresar_edad) + " años")

edad = ingresar_edad

print("De acuerdo, cuál es tu última progresión?")
achievements = {"a" : "Busqueda", "b": "encuentro", "c" : "desafio", "d" : "IDO"}

for key, value in achievements.items():
    print(key + " " + value)

#puede irse_ print(str(achievements.items()) + " \elige por favor una letra")
print("elige por favor una letra")

achievement = input()

if achievement in achievements.keys():
    achievements[achievement]
else :
    print("intentemos de nuevo")
    achievement = input()

achievement = achievements[achievement]

player = Scout(nombre, edad, achievement)
print(player)

#Aqui comienza el juego en forma
print("De acuerdo, comencemos")

#Pose the question and match the right answer
initial_score = 0
def marathon(beginning_score):
    player_score = 0
    answer_options = ["a", "b", "c", "d"]
    question_count = 1
    for question in range(1,len(trivia_list)):
        print(trivia_list[question_count][0])
        def option_array():
            option_count = 1
            incise_count = 0
            for option in range(len(answer_options)):
                print(answer_options[incise_count] + "): " + trivia_list[question][option_count])
                option_count += 1
                incise_count += 1
        option_array()
        answer = input()
        if answer in answer_options == False:
            print("debe ser una de estas resputestas:")
            option_array()
        else:
            if answer.upper() == correct_answers[str(trivia_list[question_count][0])]:
                player_score += 1
                print('Muy bien, tu puntaje ahora es ' + str(player_score))
            else:
                print("Suerte en la siguiente pregunta!")
        question_count += 1
    print("Bien, tu puntaje final es " + str(player_score))
    beginning_score = player_score
    return beginning_score

marathon(initial_score)
print(initial_score)

print(player.update_score(initial_score))

Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay