Difference between revisions of "Making AI with Python"
m |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | == Introduction == | + | ===Introduction=== |
This manual will only work if you know Python. If you don't, go learn it at: [[Getting Started With Python Programming]]. | This manual will only work if you know Python. If you don't, go learn it at: [[Getting Started With Python Programming]]. | ||
+ | |||
+ | |||
+ | |||
+ | Let's start off with a little challange: | ||
+ | |||
+ | '''Say 'hi' to the user when they greet the code, and 'bye' when the user says bye to the code.''' | ||
+ | |||
+ | |||
+ | First, we need to now what the user says. So we can send them a little input box called 'user_says': | ||
+ | |||
+ | user_says = input('Hi or Bye?') | ||
+ | |||
+ | Great! Now we need to recognise if they are saying 'Hi', or if they are saying 'Bye'. | ||
+ | |||
+ | user_says = input('Hi or Bye?') | ||
+ | if user_says == 'Hi': | ||
+ | pass | ||
+ | elif user_says == 'Bye': | ||
+ | pass | ||
+ | |||
+ | Nothing happens, no matter what you say. Let's fix that: | ||
+ | user_says = input('Hi or Bye?') | ||
+ | if user_says == 'Hi': | ||
+ | print('Hello!') | ||
+ | elif user_says == 'Bye': | ||
+ | print('Bye... :(') | ||
+ | However, if you type '''hi''' or '''bye''' (both lowercase), the code does nothing. | ||
+ | Here: | ||
+ | user_says = input('Hi or Bye?') | ||
+ | user_says = user_says.lower() | ||
+ | if user_says == 'hi': | ||
+ | print('Hello!') | ||
+ | elif user_says == 'bye': | ||
+ | print('Bye... :(') | ||
+ | Now, as long as the characters are correct (and in the right order), It will say '''Hello!''' or '''Bye... :(''' correspondingly, no matter the capitalization. | ||
+ | Simple, right? It also seems pointless, too. However, it is not: we will use this to identify what types of sentences are being said. | ||
+ | |||
+ | Take a look at this: | ||
+ | Nouns = ['car'] | ||
+ | Verbs = ['is'] | ||
+ | Adjs = ['the', 'red'] | ||
+ | Sentence = [] | ||
+ | user_says = input() | ||
+ | user_says = user_says.lower() | ||
+ | user_says = user_says.replace('.', '') | ||
+ | user_says = user_says.split(' ') | ||
+ | for i in user_says: | ||
+ | if i in Nouns: | ||
+ | Sentence.append('Noun') | ||
+ | elif i in Verbs: | ||
+ | Sentence.append('Verb') | ||
+ | elif i in Adjs: | ||
+ | Sentence.append('Adj') | ||
+ | print(Sentence) | ||
+ | Write in 'The car is red.' and see what happens! |
Latest revision as of 16:42, 15 September 2023
Introduction
This manual will only work if you know Python. If you don't, go learn it at: Getting Started With Python Programming.
Let's start off with a little challange:
Say 'hi' to the user when they greet the code, and 'bye' when the user says bye to the code.
First, we need to now what the user says. So we can send them a little input box called 'user_says':
user_says = input('Hi or Bye?')
Great! Now we need to recognise if they are saying 'Hi', or if they are saying 'Bye'.
user_says = input('Hi or Bye?') if user_says == 'Hi': pass elif user_says == 'Bye': pass
Nothing happens, no matter what you say. Let's fix that:
user_says = input('Hi or Bye?') if user_says == 'Hi': print('Hello!') elif user_says == 'Bye': print('Bye... :(')
However, if you type hi or bye (both lowercase), the code does nothing. Here:
user_says = input('Hi or Bye?') user_says = user_says.lower() if user_says == 'hi': print('Hello!') elif user_says == 'bye': print('Bye... :(')
Now, as long as the characters are correct (and in the right order), It will say Hello! or Bye... :( correspondingly, no matter the capitalization. Simple, right? It also seems pointless, too. However, it is not: we will use this to identify what types of sentences are being said.
Take a look at this:
Nouns = ['car'] Verbs = ['is'] Adjs = ['the', 'red'] Sentence = [] user_says = input() user_says = user_says.lower() user_says = user_says.replace('.', ) user_says = user_says.split(' ') for i in user_says: if i in Nouns: Sentence.append('Noun') elif i in Verbs: Sentence.append('Verb') elif i in Adjs: Sentence.append('Adj') print(Sentence)
Write in 'The car is red.' and see what happens!