CH1 2. Have a chat with computer

I still remember the feeling when CLI resonded me with "北京 晴".
This simple words respond to a universal concept of natural and pure beauty, esay to love it.
So, chat with computer, or should I say Human-Computer Interaction, that depend to the operation of INPUT function and the source code.
HOW?

Q4:How to enter the strings(as"beijing") in CLI?

The python offers the fucntion "input()".
Call this founction, the user can enter the string and save it to the parament,such as:

name = input()
Lucy

When you enter "Lucy" and press "Enter" botton, "Lucy" is assigned to the variable(name).

But how to associate word(input) with the dict?
Read the doc of LPTHW:

def find_city(themap, state):
    if state in themap:
        return themap[state]
    else:
        return "Not found."
cities['_find'] = find_city
# pay attention!
while True:
    print("state?(ENTER to quit)"),
    state = input('>')

    if not state:break

    #this line is the most important ever! study!
    city_found = cities['_find'](cities,state)
    print city_found

From this exercise I learned that:
state = input
in task ch1, input == the city name that the user enter as "北京".

Q5:How to connect the input with Help/History/Quit and print it?

Answer:

  1. Create a list to store query history,since everyone keeps saying that.

  2. Use append.() to add the search history to the list.

  3. Use len() to calculator the number of string,then use "if" to set types of decision to be made.

Altough I don't really know how they function,but I try to write some codes to connect 'input' and the weather_info text,make sure the text can be read as as the format "key" and "value"of the dict,and put "if" in it. With the help of simpleowen - who you will see I mentioned him all the time, I finished this MVP.
Basically, it looks like this way:

The rest of my code:

while (inputText!='quit' and inputText!='exit'):
    print('Please enter city name in Chinese:')
    inputText = input()
    inputText = normalize(inputText)
    if inputText in dict_weather:
       searchHistory.append(inputText)
       print(inputText,dict_weather[inputText])
    elif inputText == "history":
        if len(searchHistory) == 0:
            print("No history records.")
        else:
            for list_history in searchHistory:
                print(list_history,dict_weather[list_history])
    elif inputText == "help" or inputText == "h":
        print(''' 
        Enter city name to get the weather conditions,
        Enter help,to get more help,
        Enter history,to get the searchHistory,
        Enter quit or exit to quit.
        ''')

    elif inputText == "quit" or inputText == "exit":
        print('Goodbye!')
    else:
        print('Sorry, I do not know what you mean, pleasr try agagin.')

That's not good enough,I know.But I am so happy it runs!

results matching ""

    No results matching ""