Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python3 programming. I was wondering how do you continue to ask for user input in python? I had an assignment where I needed to create

Python3 programming. I was wondering how do you continue to ask for user input in python?

I had an assignment where I needed to create a dictionary with key values and pair values to those keys. I was able to figure that all out but I was wondering how do you continue to ask for user input? Currently, I display my key values (which in this case I did countries), then the program asks the user to type in the country they want to pick from the list, then the program outputs the paired value (which is a fun fact about that country) but after it outputs the value the only choice you have is to close the program. I would like it to ask the user to pick another country to learn another fun fact instead of having to close the program and reopen it. I know there is a way to have the program ask again if the user inputs the key-value incorrectly but that is not what I am looking for. I just would like the user to be asked if they want another fact instead of the program closing. I am also using the IDE Geany if that helps.

I was also wondering, the key values are typed with uppercase letters for the first letter. So, it would look like China but if the user inputs china that doesn't work, is there a way you can make it so it isn't case sensitive and they can type it either way and the program will still output the fun fact?

I am in the intro to programming class so this is all very new to me. Examples, if this is possible, would be appreciated!

Put my code below for reference.

# Program will output fun facts about each country the user selects

# Creating the dictionary with key values and the paired values FunFactsAboutCountries = {'Canada': 'There are more lakes in Canada than in all the other countries combined.', 'Bolivia': 'Bolivia is the flattest country on the planet.', 'Russia': 'Russia is the worlds largest country.' , 'San Marino': 'San Marino is the oldest country in the world.' , 'China': 'There are 63 million pairs of chopsticks manufactured in China every year.' }

# Prints the welcome message print("Fun Facts Around the World")

# Prints key values (countries) for x,y in FunFactsAboutCountries.items() : print (x)

# Prints asking them for input fun_fact = input("Type which state you would like to know a fun fact from: ")

found = 0 # Assign value to found to be used later

# Prints paired value (fun fact) of x (country) if entered correctly for x,y in FunFactsAboutCountries.items(): if(x==fun_fact): found=1 print(y)

# If not entered correctly prints error if(found==0): print("Error, no such key found.")

Though I did get this to work, I am also not sure why I need to assign 0 to the found value in order for it to output the error message if the user does type the country in wrong. If I do not have that assigned then it does not work. Especially since I don't have to do that with the found=1 value? But if I don't put the found=1 then my output for fun facts doesn't work correctly. Could someone clarify why exactly it has to be that way?

Hopefully, that makes sense for what I am looking for, thanks again!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago