I wrote the code below.
I would love to understand the following:
How do I random select a key from a dictionary without repeating it again.
Why doesn’t it exit out if the user answer is 0==>there’s a break in the loop
How do i dismiss CAP letter case==> for example if i type berlin it will be accepted as a wrong answer and if I type Berlin it will be accepted as the correct answer
Why doesn’t it continue looping in case the user answer is a number=>there’s a continue command==> for example if the user entered a number i would like to tell him it’s not valid and ask the same question with the same random key
Would appreciate your input
Riddles=int(input("How many riddles would you like to solve?\n"))
my_dict = {
'Belgium': 'Brussels',
'Bulgaria': 'Sofia',
'Denmark ': 'Copenhagen',
'Germany': 'Berlin',
'France': 'Paris',
'England': 'London',
}
# get a random Key from the dictionary
import random
x=0
D=0
for i in range(Riddles):
key = random.choice(list(my_dict))
print('What is the capital of', key,'?')
useranswer=input('')
if useranswer==0:
print('You have chosen to quit')
break
elif not useranswer.isalpha():
print('Numbers are not allowed, use only the alphabet')
continue
elif useranswer == my_dict[key]:
print(my_dict[key],'is correct')
x=x+1
elif useranswer != my_dict[key]:
print(useranswer,'is not correct')
print('The correct answer is', my_dict[key])
D=D+1
avg=(x/(i+1))*100
print('You had', x, 'correct answers and', D, 'not correct from total',i+1,'riddles' )
print(avg,'% answered correctly')
If you make a list of the keys outside of the loop, you can remove the key from the list once it is used to prevent it from being reused.
0 != "0" - you need to convert the input str to int like you do at the start for Riddles
You can lowercase strings using the .lower() method the same way you use .isalpha()
continue skips to the next iteration of the loop and should ask for a new capital if I’m reading this correctly. In order to ask again for the same key without using up one of your riddle count you would need to rework the logic.
Thank You for your kind explanation.
I appreciate your way of thinking and logic.
It works PERFECT now (see below).
#only dic
Riddles=int(input("How many riddles would you like to solve?\n"))
my_dict = {
'Belgium': 'Brussels',
'Bulgaria': 'Sofia',
'Denmark': 'Copenhagen',
'Germany': 'Berlin',
'France': 'Paris',
'England': 'London',
}
# get a random key from the dictionary
import random
x=0
D=0
for i in range(3):
key = random.choice(list(my_dict))
print('What is the capital of', key,'?')
useranswer=input('')
if useranswer=='0':
print('You have chosen to quit')
break
elif not useranswer.isalpha():
print('Numbers are not allowed, use only the alphabet')
useranswer = input('')
if useranswer == my_dict[key].lower() or useranswer == my_dict[key]:
print(my_dict[key],'is correct')
x=x+1
del my_dict[key]
elif useranswer != my_dict[key]:
print(useranswer,'is not correct')
print('The correct answer is', my_dict[key])
D=D+1
del my_dict[key]
#
elif useranswer == my_dict[key].lower() or useranswer == my_dict[key]:
print(my_dict[key],'is correct')
x=x+1
del my_dict[key]
elif useranswer != my_dict[key]:
print(useranswer,'is not correct')
print('The correct answer is', my_dict[key])
D=D+1
del my_dict[key]
avg=(x/(i+1))*100
print('You had', x, 'correct answers and', D, 'not correct from total',i+1,'riddles' )
print(avg,'% answered correctly')