Can somebody help me to show two different things for two different cities? | Sololearn: Learn to code for FREE!

-1

Can somebody help me to show two different things for two different cities?

I am trying to build a weather program in Python, which will tell the temperature to the users of different cities, depending on the time. I want the program to ask the user where they are from(which the program does), but I also want that if the user type different cities, the program should show that, so far I have weather information of only one city out of three. Can somebody help me to tell what to do? Thank you print("Welcome, before starting, let`s authenticate you") name = str(input("Please enter your name: ")) age = int(input("Please enter your age: ")) if age < 18: print("Your age is smaller") quit() else: print("Your age is allowed") current_country = str(input("Please enter the current country you are living: ")) country_name = ["United Kingdom", "England", "UK"] if current_country in country_name: print() else: print("Wrong country") quit() city_where_he_lives = str(input("Please enter the city where you live: ")) if city_where_he_lives == "Bradford": print() elif city_where_he_lives == "Birmingham": print() elif city_where_he_lives == "London": print() else: print("Currently we have the temperature of Bradford, Birmingham and London, we will update with more cities soon") quit() cities_available = ["Bradford", "Birmingham", "London"] if city_where_he_lives not in cities_available: print("Currently we have the temperature of Bradford, Birmingham and London, we will update with more cities soon") quit() else: print() time = input("Please enter the current time: ") if time.lower() == "1 pm": print("14 °C") elif time.lower() == "2 pm": print("15 °C") elif time.lower() == "3 pm": print("16 °C") elif time.lower() == "4 pm": print("17 °C") elif time.lower() == "5 pm": print("17 °C") elif time.lower() == "6 pm": print("17 °C") elif time.lower() == "7 pm": print("17 °C") elif time.lower() == "8 pm": print("15 °C") elif time.lower() == "9 pm": print("14 °C") elif time.lower() == "10 pm": print("12 °C") elif time.lowe

5/19/2022 1:43:12 PM

Danish Zubair

16 Answers

New Answer

+3

Bob_Li I think you're jumping in too early talking about APIs. If you look at his code, he's a beginner and he's trying to figure this out in a simpler way which is good. Let's not make someone cook a 5-course meal before they even know how to properly make use of their tools and utensils. Baby steps.

+3

Danish Zubair Don't try to master a language. It is not possible and you will burnout and lose interest if you try. Focus less on syntax and more on problem solving. Make sure you're writing pseudocode before you even code. Like if you wrote pseudocode like: - Have user enter city and time - City gives temperature depending on time. With just those two lines, you could've asked how to implement and it would've been easier to give an answer in quicker. But it's okay, because in the beginning it can be hard to know which questions to ask, so no biggie. And just like with this example that you're trying to program, you should definitely be focusing more on the fundamentals of compsci such as data structures. Googling the specific syntax for implementing a map (dictionary) is easy but when it's the best to use is something you need to learn. Knowing how a computer works under the hood and the tools it has will help you understand ANY language since they're mostly the same computers.

+2

Justice Thank you for you kind advice.

+1

Bob_Li yeah, I just learned them today, I need to practice in order to get familiar. Thank you sooo much for your advice and explanation.

+1

Bob_Li ok

0

Have you written any pseudocode for this? You don't have many comments so it's a little hard understanding why you want to do certain things. Don't skip pseudocode because that'll help you break down what you want to do. Break the problem into some smaller chunks.

0

I just wanted to ask how can I access data where the "time" variable starts

0

It looks like you are doing so by means of your if statements. What's the output you're getting and maybe if you can share the code as a code bit, it will be a little more easier to read.

0

Because the code was lenghty, Sololearn cut 4 lines of code, the code works well, but what I want is that I want the code to ask the user if he wants Bradford city's weather, Birmingham city's weather or London city's weather, and after that I want the code to display different weather temperatures, but because of this issue, I have knly Bradfird's weather information. I tried to code: If city_where_he_lives == "Bradford": print( When I arrived at print, I did not know what to do in order to access weather information for Bradford.

0

It sounds like you can make use of some data structures instead of just storing everything into a variable. That's not really going to work. Try to use some dictionaries since they make use of key-value pairs. So you can name one dictionary after a city and then use the time as a key and the temperature as a value. That way also, you don't have so many if statements.

0

a more useful one is based on weather data api, most have free option, but you have to register for user key. Good enough for learning. Here is a nice simple tutorial. https://www.tutorialspoint.com/find-current-weather-of-any-city-using-openweathermap-api-in-python

0

Justice right.😁 walk before you run. at any rate, it is good to keep in mind for future projects.

0

Danish Zubair Are you familiar with dictionaries in Python? As Justice said: It will be a good idea to organize your data in dictionaries. something like: Bradford = {"1 pm" : "14 °C", "2 pm" : "14 °C", .... } and make a dict of the dicts🙄 choices = {"Bradford":Bradford, ...} (just tried it, you can't compare string with variable names...)

0

Justice thank you soooo much for your advice, I am going to use it now. You guys guessed, I am a beginner who needs to masterise Python in 2 months in order to go to the next course. It is going to be challenging, if you or bob_li have any advice, please tell me as I am hopeless. Thank you soo much for your dictionary advice, though.

0

Danish Zubair do that and it just becomes a problem of directing the user to the answer.

0

maybe this template can help you? https://code.sololearn.com/cM3doJ5ZsUSO/?ref=app