Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Homework 6
class BasketballPlayer:
def __init__(self, first_name, last_name, height_cm, weight_kg):
self.first_name = first_name
self.last_name = last_name
self.height_cm = height_cm
self.weight_kg = weight_kg
with open("player_database.txt", "w") as database:
database.write(str(self.__dict__))
print("New player you entered is: " + str(self.__dict__))
new_player = BasketballPlayer(
first_name=input("Please enter the player's name here: "),
last_name=input("Please enter the player's last name here: "),
height_cm=input("Please enter the player's height in cm here: "),
weight_kg=input("Please enter the player's weight in kg here: "))
# Why doesn't json work with .append in this case?
# How to save more than one player into the file with this method?
# Could this be solved with the pickle function?
# What kind of file can you pickle things into?
import random
import json
import datetime
class Result:
def __init__(self, name, attempts, time):
self.name = name
self.attempts = attempts
self.time = time
with open("result_storage.txt", "w") as database:
database.write(str(self.__dict__))
print("Your score is: " + str(self.__dict__))
def play_game(level):
secret = random.randint(1, 30)
print(secret)
attempts = 0
wrong_guesses = []
players_name = str(input("What is your name? Type here:"))
while True:
guess = int(input("Guess the secret number (between 1 and 30): "))
attempts += 1
if guess == secret:
print("You've guessed it - congratulations! It's number " + str(secret))
print("Attempts needed: " + str(attempts))
current_time = datetime.datetime.now()
current_time_formatted = current_time.strftime("%d.%m.%Y, %H:%M:%S")
result_one = Result(name=players_name, attempts=attempts, time=current_time_formatted)
break
elif guess > secret:
wrong_guesses.append(guess)
if level == "easy":
print("Your guess is not correct... try something smaller")
elif guess < secret:
wrong_guesses.append(guess)
if level == "easy":
print("Your guess is not correct... try something bigger")
def goodbye_message():
print("Thanks for playing! Bye!")
while True:
selection = input("Would you like to A) play a new game or B) quit?")
if selection == "A":
level = input("Choose 'easy' or 'hard' mode!")
play_game(level)
if selection == "B":
goodbye_message()
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.