Difference between revisions of "Making AI with Python"

(Introduction)
m
 
(5 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]].
===Program Example 1===
 
  
'''Print all two digit positive integers <math>x</math> such that <math>5x</math> is a three digit positive integer.'''
 
  
We can keep our code and modify some parts of it.
 
 
def check(a, min, max):
 
    if a*5 > min - 1 and a*5 < max + 1:
 
        return True 
 
    else:
 
        return False
 
 
def print_check(range_min, range_max, check_min, check_max):
 
    for i in range(range_min, range_max + 1):
 
        if check(i, check_min, check_max):
 
            print(i)
 
    return
 
 
print_check(10, 99, 100, 999)
 
  
Why did we add so many functions?
+
Let's start off with a little challange:
  
Well, if the numbers in a problem change (and the words stay the same), and you need to change a lot of numbers in your program, your program is considered '''hard-coded'''. We want our programs to be as '''soft-coded''' as possible. In our new program, we only need to change 4 numbers (in the print_check() statement) if the numbers in the problem change. Therefore, our program is relatively soft-coded. There are still ways to soft-code this program even more, though.
+
'''Say 'hi' to the user when they greet the code, and 'bye' when the user says bye to the code.'''
  
If we run our program, we get our answer.
 
  
All numbers from 20 to 99 work!
+
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!