The Student Room Group

Help with my python code- functions

Hi,
I made a fairly simple code in python and i would want to make it better by putting functions into it. Could someone look over my code and see if I can do this?
if someone could could you dm me and i could send it or show it somehow.
Thanks
You can write/copy and paste it in [NOPARSE][/NOPARSE] tags.

eg.
def function():
pass
Reply 2
#test of logical signs#>, <, ==, !=
#8 questions
import random
user_name = input("===Welcome to the game=== \n ===Enter your name===\n :")
counter = 0
score = 0
while counter != 8: #only 8 questions
counter = counter + 1
number_one = random.randint (1,11) #number between 1 and 10
number_two = random.randint (1,11)
logical_sign = ['>','<','] #logic signs which user can input
random_sign = random.choice(logical_sign) #will generate a random from the given logical signs
print ("===",number_one, random_sign, number_two,"===") #prints out the problem


user_ans = input("===Please enter your answer 'False' or 'True=== \n").lower()
if random_sign == '>':
if number_one > number_two:
answer = 'true'

else:
answer = 'false'



elif random_sign == '<':
if number_one < number_two:
answer = 'true'

else:
answer = 'false'



elif random_sign == '==':
if number_one == number_two:
answer = 'true'

else:
answer = 'false'



elif random_sign == '!=':
if number_one != number_two:
answer = 'true'

else:
answer = 'false'



if user_ans == answer: #if the user input equals to the real answer of the sum give point
print ("Well Done")
score = score + 1


else:
print ("Sorry Wrong") #user gets no points


print (user_name, "===You have finished the game===")
print ("===Your score is ",score,"/8===")

Original post by Undec
#test of logical signs#>, <, ==, !=
#8 questions
import random
user_name = input("===Welcome to the game=== \n ===Enter your name===\n :")
counter = 0
score = 0
while counter != 8: #only 8 questions
counter = counter + 1
number_one = random.randint (1,11) #number between 1 and 10
number_two = random.randint (1,11)
logical_sign = ['>','<','] #logic signs which user can input
random_sign = random.choice(logical_sign) #will generate a random from the given logical signs
print ("===",number_one, random_sign, number_two,"===") #prints out the problem


user_ans = input("===Please enter your answer 'False' or 'True=== \n").lower()
if random_sign == '>':
if number_one > number_two:
answer = 'true'

else:
answer = 'false'



elif random_sign == '<':
if number_one < number_two:
answer = 'true'

else:
answer = 'false'



elif random_sign == '==':
if number_one == number_two:
answer = 'true'

else:
answer = 'false'



elif random_sign == '!=':
if number_one != number_two:
answer = 'true'

else:
answer = 'false'



if user_ans == answer: #if the user input equals to the real answer of the sum give point
print ("Well Done")
score = score + 1


else:
print ("Sorry Wrong") #user gets no points


print (user_name, "===You have finished the game===")
print ("===Your score is ",score,"/8===")


You aren't reusing any code, so functions aren't really necessary. That being said, the code below reads a little better. There are a few other ways to make it read better eg. using tuple assignment to assign values to multiple things and using counter += 1 instead of counter = counter + 1.


#test of logical signs#>, <, ==, !=
#8 questions
import random
user_name = input("===Welcome to the game=== \n ===Enter your name===\n :")
counter = 0
score = 0
def find_answer(random_sign, number_one, number_two):
if random_sign == '>':
if number_one > number_two:
return 'true'
else:
return 'false'
elif random_sign == '<':
if number_one < number_two:
return 'true'
else:
return 'false'
elif random_sign == '==':
if number_one == number_two:
return 'true'
else:
return 'false'
elif random_sign == '!=':
if number_one != number_two:
return 'true'
else:
return 'false'
def iterate():
global score
number_one = random.randint (1,11) #number between 1 and 10
number_two = random.randint (1,11)
logical_sign = ['>','<','] #logic signs which user can input
random_sign = random.choice(logical_sign) #will generate a random from the given logical signs
print ("===",number_one, random_sign, number_two,"===") #prints out the problem
user_ans = input("===Please enter your answer 'False' or 'True=== \n").lower()
answer = find_answer(random_sign, number_one, number_two)
if user_ans == answer: #if the user input equals to the real answer of the sum give point
print ("Well Done")
score = score + 1
else:
print ("Sorry Wrong") #user gets no points

while counter != 8: #only 8 questions
counter = counter + 1
iterate()

print (user_name, "===You have finished the game===")
print ("===Your score is ",score,"/8===")
Original post by morgan8002
You can write/copy and paste it in [NOPARSE][/NOPARSE] tags.

eg.def function():
pass


Is this OCR computing?
Original post by xGCSE_Studentx
Is this OCR computing?


I don't know. Ask the OP.
Reply 6
Original post by morgan8002
I don't know. Ask the OP.


yes ocr gcse computing
Reply 7
Original post by morgan8002
You aren't reusing any code, so functions aren't really necessary. That being said, the code below reads a little better. There are a few other ways to make it read better eg. using tuple assignment to assign values to multiple things and using counter += 1 instead of counter = counter + 1.


#test of logical signs#>, <, ==, !=
#8 questions
import random
user_name = input("===Welcome to the game=== \n ===Enter your name===\n :")
counter = 0
score = 0
def find_answer(random_sign, number_one, number_two):
if random_sign == '>':
if number_one > number_two:
return 'true'
else:
return 'false'
elif random_sign == '<':
if number_one < number_two:
return 'true'
else:
return 'false'
elif random_sign == '==':
if number_one == number_two:
return 'true'
else:
return 'false'
elif random_sign == '!=':
if number_one != number_two:
return 'true'
else:
return 'false'
def iterate():
global score
number_one = random.randint (1,11) #number between 1 and 10
number_two = random.randint (1,11)
logical_sign = ['>','<','] #logic signs which user can input
random_sign = random.choice(logical_sign) #will generate a random from the given logical signs
print ("===",number_one, random_sign, number_two,"===") #prints out the problem
user_ans = input("===Please enter your answer 'False' or 'True=== \n").lower()
answer = find_answer(random_sign, number_one, number_two)
if user_ans == answer: #if the user input equals to the real answer of the sum give point
print ("Well Done")
score = score + 1
else:
print ("Sorry Wrong") #user gets no points

while counter != 8: #only 8 questions
counter = counter + 1
iterate()

print (user_name, "===You have finished the game===")
print ("===Your score is ",score,"/8===")


Thank's a lot I can see the logic behind it and i understand it more.
I have never heard of Tuples but I will look into it and try to add it in.
Thanks for helping :biggrin: :biggrin::biggrin:
Original post by Undec
Thank's a lot I can see the logic behind it and i understand it more.
I have never heard of Tuples but I will look into it and try to add it in.
Thanks for helping

There are many kinds of lists in Python. You have vectors, sets, strings, byte arrays, dictionaries and tuples, each with their own specific properties. Tuples are just another kind of list.
Tuples are immutable and ordered. They have built in indices like eg. vectors and strings do. The elements can be any data structure. That's all there is to them really. When creating a tuple, we use commas and sometimes use brackets too.
So if I want to make a tuple a with the elements 3, 5, {6, 4, 2}, 1, 0, "" and [1, 2, 3, 4] I will write the following code:
a = (3, 5, {6, 4, 2}, 1, 0, "", [1, 2, 3, 4]) or a = 3, 5, {6, 4, 2}, 1, 0, "", [1, 2, 3, 4]
There are a few things that you can do with tuples that make for cleaner and shorter code.

The first I'll mention is that you can assign multiple variables multiple values at once.
x, y = 2, 1
Now x has the value 2 and y has the value 1. You've created two new variables with two different values with one line of code.
Another interesting thing that you can do with this is swap the values of two variables. Without tuples this takes several lines of code.
a = 3
b = "g"
a, b = b, a

The last line switches the values of a and b so now a has the value "g" and b has the value 3.

The second thing I'll mention is that you can use tuples to return multiple variables from a function. You can do this using vectors but it gets more messy.
def addtake(a, b):
return a + b, a - b

The above function is pretty useless, but this tool is often useful.

Quick Reply

Latest