Ваша проблема в том, что ваш цикл while не прерывается с правильным вводом.Разделение этого на две функции облегчает:
def get_cq(msg):
while True:
try:
d = int(input(msg))
if 1 <= d <= 7:
return d
else:
print('Please enter a positive integer between 1 and 7.')
except ValueError:
print('Please enter a positive integer between 1 and 7.')
def continent_question():
continent = {'0': None, 1: 'asia', 2: 'africa',
3: 'north america', 4: 'south america',
5: 'europe', 6: 'oceania', 7: 'antarctica'}
print('Which continent would you like to travel to?')
print(' 1)', continent[1].title())
print(' 2)', continent[2].title())
print(' 3)', continent[3].title())
print(' 4)', continent[4].title())
print(' 5)', continent[5].title())
print(' 6)', continent[6].title())
print(' 7)', continent[7].title())
continent_input = get_cq('Which continent would you like to travel to? ')
return continent[continent_input]
x = continent_question()
print('Your answer is', x)
Редактировать: чтобы ответить на ваш вопрос, я всегда делал проверку ввода с помощью отдельной функции, потому что я нахожу это беспорядочным без одной.Помните, что вы можете определить одну функцию внутри другой, например:
def continent_question():
def get_cq(msg):
while True:
try:
d = int(input(msg))
if 1 <= d <= 7:
return d
else:
print('Please enter a positive integer between 1 and 7.')
except ValueError:
print('Please enter a positive integer between 1 and 7.')
continent = {'0': None, 1: 'asia', 2: 'africa',
3: 'north america', 4: 'south america',
5: 'europe', 6: 'oceania', 7: 'antarctica'}
print('Which continent would you like to travel to?')
print(' 1)', continent[1].title())
print(' 2)', continent[2].title())
print(' 3)', continent[3].title())
print(' 4)', continent[4].title())
print(' 5)', continent[5].title())
print(' 6)', continent[6].title())
print(' 7)', continent[7].title())
continent_input = get_cq('Which continent would you like to travel to? ')
return continent[continent_input]
x = continent_question()
print('Your answer is', x)
, чтобы не загромождать остальную часть вашего кода.
2-й вопрос:
#/9213715/proverka-vvoda-v-funktsii
def continent_question():
def get_cq(msg):
while True:
try:
d = input(msg)
if 1 <= int(d) <= 7:
return int(d)
else:
print(f'\'{d}\' is an invalid input. Please enter a positive integer between 1 and 7. Your Options:')
print(' 1)', continent[1].title())
print(' 2)', continent[2].title())
print(' 3)', continent[3].title())
print(' 4)', continent[4].title())
print(' 5)', continent[5].title())
print(' 6)', continent[6].title())
print(' 7)', continent[7].title())
except ValueError:
print(f'\'{d}\' is an invalid input. Please enter a positive integer between 1 and 7. Your Options:')
print(' 1)', continent[1].title())
print(' 2)', continent[2].title())
print(' 3)', continent[3].title())
print(' 4)', continent[4].title())
print(' 5)', continent[5].title())
print(' 6)', continent[6].title())
print(' 7)', continent[7].title())
continent = {'0': None, 1: 'asia', 2: 'africa',
3: 'north america', 4: 'south america',
5: 'europe', 6: 'oceania', 7: 'antarctica'}
print('Which continent would you like to travel to?')
print(' 1)', continent[1].title())
print(' 2)', continent[2].title())
print(' 3)', continent[3].title())
print(' 4)', continent[4].title())
print(' 5)', continent[5].title())
print(' 6)', continent[6].title())
print(' 7)', continent[7].title())
continent_input = get_cq('Which continent would you like to travel to? ')
return continent[continent_input]
x = continent_question()
print('Your answer is', x)