Loops and random command from a dictionary

I wrote the code below.
I would love to understand the following:

1. How do I random select a key from a dictionary without repeating it again.
2. Why doesn’t it exit out if the user answer is 0==>there’s a break in the loop
3. 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
4. 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

``````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,'?')
print('You have chosen to quit')
break
print('Numbers are not allowed, use only the alphabet')
continue
print(my_dict[key],'is correct')
x=x+1
D=D+1
avg=(x/(i+1))*100
print('You had', x, 'correct answers and', D, 'not correct from total',i+1,'riddles' )
``````
1. 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.

2. `0 != "0"` - you need to convert the input str to int like you do at the start for `Riddles`

3. You can lowercase strings using the `.lower()` method the same way you use `.isalpha()`

4. `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.

1 Like

For (1) You can use random.shuffle on `list(my_dict.keys())` to get `shuffled_keys`. Then loop `for i, key in zip(range(Riddles), shuffled_keys)`.

The line `key = random.choice(list(my_dict))` wouldn’t be needed in that case, since the `random.shuffle` already did the randomization.

For (4) you can put the contents of that `elif` together with line that asks for the `useranswer` inside a `while`. Something like

``````useranswer = input('')
print('Numbers are not allowed, use only the alphabet')
``````
1 Like

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,'?')
print('You have chosen to quit')
break
print('Numbers are not allowed, use only the alphabet')
print(my_dict[key],'is correct')
x=x+1
del my_dict[key]

D=D+1
del my_dict[key]

#