Reputation: 1
I am making a quiz game that alternates between asking for capital cities and countries. They are supposed to alternate randomly thus I am using a random number generator however the if statements wont repeat according to the output of the random number generator. (I'm pretty new to coding, sorry if this is long)
This is the code:
import random
from random import randrange
quest_one = int(input('How many questions? '))
for i in range(quest_one):
number = (randrange(2))
Country_main = {
"Australia" : 'Canberra',
'india' : 'New Delhi',
'France' : 'Paris',
'Japan' : 'Tokyo',
'China': 'Beijing',
'USA': 'Washington',
'England': 'London',
'Russia': 'Moscow',
'Germany': 'Berlin',
'Brazil': 'Brasilia',
'Bangladesh': 'Dhaka'
}
Capital_main = {
'Canberra': 'Australia',
'New Delhi': 'India',
'Paris': 'France',
'Tokyo': 'Japan',
'Beijing': 'China',
'Washington': 'USA',
'London': 'England',
'Moscow': 'Russia',
'Berlin': 'Germany',
'Brasilia': 'Brazil',
'Dhaka': 'Bangladesh'
}
def Capital_of():
rando = random.choice(list(Country_main.items()))
ans = input("What is the capital city of " + rando[0] + '? ')
if rando[1] in ans:
print('correct')
#Change to add value to score later
elif rando[1] not in ans:
print('wrong')
return
def Country_of():
randy = random.choice(list(Capital_main.items()))
ansy = input("Which country has the capital city " + randy[0] + '? ')
if randy[1] in ansy:
print('correct')
# Change to add value to score later
elif randy[1] not in ansy:
print('wrong')
return
if number == 1:
Capital_of()
if number == 0:
Country_of()
Upvotes: 0
Views: 54
Reputation: 802
The issue lies in the way you have your code structured. Your for
loop should be the main loop that asks the specified number of questions.
The way you have it setup right now, the for loop is generating x
amount of random numbers and then it exits the loop and runs the rest of the program.
# current program
for i in range(quest_one):
number = (randrange(2))
#
# more code
#
if number == 1:
Capital_of()
if number == 0:
Country_of()
Your if
statements need to be nested inside of the for
loop inorder to keep asking questions.
I reformatted your code so the if
statement is nested in the for
loop.
Try this out:
import random
from random import randrange
Country_main = {
"Australia" : 'Canberra',
'india' : 'New Delhi',
'France' : 'Paris',
'Japan' : 'Tokyo',
'China': 'Beijing',
'USA': 'Washington',
'England': 'London',
'Russia': 'Moscow',
'Germany': 'Berlin',
'Brazil': 'Brasilia',
'Bangladesh': 'Dhaka'
}
Capital_main = {
'Canberra': 'Australia',
'New Delhi': 'India',
'Paris': 'France',
'Tokyo': 'Japan',
'Beijing': 'China',
'Washington': 'USA',
'London': 'England',
'Moscow': 'Russia',
'Berlin': 'Germany',
'Brasilia': 'Brazil',
'Dhaka': 'Bangladesh'
}
def main():
num_questions = int(input('How many questions? '))
for i in range(num_questions):
number = (randrange(2))
if number == 1:
capital_of()
else:
country_of()
def capital_of():
rando = random.choice(list(Country_main.items()))
ans = input("What is the capital city of " + rando[0] + '? ')
if rando[1] in ans:
print('correct')
#Change to add value to score later
elif rando[1] not in ans:
print('wrong')
return
def country_of():
randy = random.choice(list(Capital_main.items()))
ansy = input("Which country has the capital city " + randy[0] + '? ')
if randy[1] in ansy:
print('correct')
# Change to add value to score later
elif randy[1] not in ansy:
print('wrong')
return
if __name__ == '__main__':
main()
Upvotes: 1