EasyC
EasyC

Reputation: 9

Adding a new name/item to a list and calling that variable globally?

I'm pretty much brand new to coding and decided to start with Python. I have been working on a simple "Number Guessing Game" and have evolved it a bit more each iteration.

I decided that I want to make it a little bit more robust that just finishing after the number is guessed.

I want the user/player to enter their name, have that name stored, and then used throughout... until the end, when they're asked if they want to play again and if it's the 'same user?" and if they say, "No.", I want it to append a new name to the list and then go through the game again.

This is what I've got so far... please be gentle... I know there's some errors :(

        import sys
        import os
        import random
        global player


        while True:
            def GuessingGame():
        player = []
        player.append(input("Please Enter Your Name:\n")),
        # player_name = input("Please enter your name: \n")
        print(f"Hello, {player[0]}! Let's get started!\n")
        number = 500
        count = 1
        guess = int(input("Guess a number between 1 and 1000: \n"))

        while guess != number:
            count += 1
            if guess > (number + 10):
            print("TOO HIGH, a little lower...")
            elif guess < (number - 10):
                print("TOO LOW... guess higher!")
            elif guess > (number + 5):
                print("You're getting warmer, but still TOO HIGH, a little lower...")
            elif guess < (number - 5):
                print("You're getting warmer, but still TOO LOW...  higher!")
            elif guess < number:
                print("You're REALLY warm, but still a bit TOO LOW")
            elif guess > number:
                print("You're REALLY, but still a bit TOO HIGH")
            guess = int(input("Try again...\n"))
        if guess == number:
            print(f"You got it!!! It took you {count} tries to guess {number}\n")
            response1 = str(input("Would you like to try again? Y or N\n"))
            if response1 == ("y" or "Y"):
                 sameplayer = str(input("Is this still {player[0]}? Y1 or N1\n"))
                 if sameplayer == ("y1" or "Y1"):
                    GuessingGame()
                 else:
                    newplayer = player.extend(str(input("Please Enter New Player's Name:\n")))
                    print(f"Hello, {player[1]}!, Let's Get Started!")

        return
        GuessingGame()

Again, this is the first time I'm sharing anything I've done... so please don't just wreck me :)

Upvotes: 0

Views: 37

Answers (1)

diegoiva
diegoiva

Reputation: 484

Here i copy the same code you wrote, but with a little bit of modifications that i felt were needed to be made.

from random import randint

player = None

def GuessingGame():
    global player
    if player is None:
        player = input("Please Enter Your Name:\n")
    print(f"Hello, {player}! Let's get started!\n")
    number = randint(1, 1000)
    guess = int(input("Guess a number between 1 and 1000: \n"))
    count = 1
    while guess != number:
        if guess > (number + 10):
            print("TOO HIGH, a little lower...")
        elif guess < (number - 10):
            print("TOO LOW... guess higher!")
        elif guess > (number + 5):
            print("You're getting warmer, but still TOO HIGH, a little lower...")
        elif guess < (number - 5):
            print("You're getting warmer, but still TOO LOW...  higher!")
        elif guess < number:
            print("You're REALLY warm, but still a bit TOO LOW")
        elif guess > number:
            print("You're REALLY, but still a bit TOO HIGH")
        if guess == number:
            break
        else:
            count += 1
            guess = int(input("Try again...\n"))

    print(f"You got it!!! It took you {count} tries to guess {number}\n")
    response1 = str(input("Would you like to try again? Y or N\n"))
    if response1.lower() == 'y':
        sameplayer = str(input(f"Is this still {player}? Y or N\n"))
        if sameplayer.lower() == 'n':
            player = None
            return
        else:
            return

while True:
    GuessingGame()

Let me know if you got any questions.

Upvotes: 0

Related Questions