-1

Why does this not work? I initialize score and count to 0 at the start. But they cannot be used in the function? There is an error? Why is this and is there a simple solution to this?

import random

capitals = ["New Dehli","Washington DC", "Tokyo", "London", "Lisbon", "Madrid", "Paris", "Berlin", "Warsaw", "Kiev", "Moscow", "Prague", "Rome"]
countries = ["India", "USA" ,"Japan" ,"England","Portugal", "Spain", "France", "Germany", "Poland", "Ukraine", "Russia", "Czech Republic", "Italy"]

score , count = 0 , 0
play = input('Would you like to play? Yes or No: ').lower()
while play == 'yes':
    def main():
        index = random.randrange(0, 13)
        guess = input(f"What is the capital of {countries[index]} ?: ").lower()
        count += 1
        if guess == capitals[index].lower():
            print("You guessed correct!")
            score += 1
        else:
            print(f"Incorrect, correct answer is {capitals[index]}.")
    main()
    play = input('Would you like to play again? Yes or No: ').lower()
print(f"Your guessed {score} correctly out of {count}!")
quit()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Hissan
  • 109
  • 1
  • This is bad, do not declare a function inside a while loop. Do it outside, then call `main()` inside the loop. – Carl HR Jun 21 '22 at 12:54
  • won't fix your var scope, but you should define the function outside of the `while` loop... otherwise it gets redefined every time through the loop – Anentropic Jun 21 '22 at 12:54
  • Also, to use global variables, you must tell python about it using the global keyword. Just append this on the start of main: `global count` and `global score`. Now you can modify both variables from the main scope. – Carl HR Jun 21 '22 at 12:56
  • One way of not using global variables, which is more advisable, is by passing `count` and `score` as parameters to `main()` like this: `main(score, count)`. Incrementing them inside main just like you already do, then return them to the while-loop scope. – Carl HR Jun 21 '22 at 13:02

1 Answers1

0

This is a way of writing your script without the need of using global variables:

import random

capitals = ["New Dehli","Washington DC", "Tokyo", "London", "Lisbon", "Madrid", "Paris", "Berlin", "Warsaw", "Kiev", "Moscow", "Prague", "Rome"]
countries = ["India", "USA" ,"Japan" ,"England","Portugal", "Spain", "France", "Germany", "Poland", "Ukraine", "Russia", "Czech Republic", "Italy"]

score, count = 0, 0

def main(score, count):
    index = random.randrange(0, 13)
    guess = input(f"What is the capital of {countries[index]} ?: ").lower()
    count += 1
    if guess == capitals[index].lower():
        print("You guessed correct!")
        score += 1
    else:
        print(f"Incorrect, correct answer is {capitals[index]}.")

    return score, count

play = input('Would you like to play? Yes or No: ').lower()
while play == 'yes':
    score, count = main(score, count)
    play = input('Would you like to play again? Yes or No: ').lower()
print(f"Your guessed {score} correctly out of {count}!")
quit()

But... If you really need to use global variables, you can use the global keyword:

import random

capitals = ["New Dehli","Washington DC", "Tokyo", "London", "Lisbon", "Madrid", "Paris", "Berlin", "Warsaw", "Kiev", "Moscow", "Prague", "Rome"]
countries = ["India", "USA" ,"Japan" ,"England","Portugal", "Spain", "France", "Germany", "Poland", "Ukraine", "Russia", "Czech Republic", "Italy"]

score, count = 0, 0

def main():
    global score
    global count

    index = random.randrange(0, 13)
    guess = input(f"What is the capital of {countries[index]} ?: ").lower()
    count += 1
    if guess == capitals[index].lower():
        print("You guessed correct!")
        score += 1
    else:
        print(f"Incorrect, correct answer is {capitals[index]}.")

play = input('Would you like to play? Yes or No: ').lower()
while play == 'yes':
    main()
    play = input('Would you like to play again? Yes or No: ').lower()
print(f"Your guessed {score} correctly out of {count}!")
quit()
Carl HR
  • 776
  • 5
  • 12