Reputation: 1
I have to make a maths test in Python for school but instead of a question like 6 + 8?
, it comes out like this (6, '+', 8)
.
Here is my code:
print ('What is your name?')
name = input()
print (' Hello ' + name +', Welcome to the python maths quiz by Tom Luxton, you will be asked 10 maths questions and marked out of 10 at the end, good luck! ')
import random
ops = ['+', '-', '*']
num1 = random.randint(1,12)
num2 = random.randint(1,10)
op = random.choice(ops)
answer = num1, op, num2
print(answer)
Upvotes: 0
Views: 78
Reputation: 1124558
You are printing a tuple, answer
. Either print the individual elements or turn that tuple into a string first.
Printing the elements of the tuple can be done by passing them to print()
using the *args
syntax:
print(*answer)
or by not creating a tuple in the first place but by instead passing the arguments to print()
separately:
print(num1, op, num2)
You could turn the tuple into a string first by mapping all values to strings and joining them manually with a space:
answer = ' '.join(map(str, answer))
or you could use string formatting with str.format()
:
answer = '{} {} {}?'.format(num1, op, num2)
which has the added advantage that it is now easy to add that ?
question mark you wanted.
Upvotes: 5