Reputation: 1
This works, but I feel it's not efficient to define a new variable just for a print call. IS there a better method to take an input and print it as a string. Then convert it to an int and possibly back yet again to a str? I read up on the % command but don't believe I'm incorporating it correctly.
print ('This is a program to choose two numbers adding up to ten')
firstInput = input(" Pick a number? ")
firstNumber = int (firstInput)
print ('Ok the base number is ' + firstInput + ' so make that 10')
newNumber = int(input(' Number to add? '))
if newNumber + firstNumber == 5 + 5:
print ("That worked!")
if newNumber + firstNumber != 5 + 5:
print ("That didn't work!")`
Upvotes: 0
Views: 109
Reputation: 1122232
Use string formatting to interpolate integers:
print('Ok the base number is {} so make that 10'.format(firstNumber))
You can do much more complex formatting, but in this basic example the value for the first slot ({}
) is taken from the first argument (firstNumber
), which automatically is converted to a string with the str()
built-in function.
As a complete program, with a few small optimizations:
print ('This is a program to choose two numbers adding up to ten')
firstNumber = int(input(" Pick a number? "))
print('Ok the base number is {} so make that 10'.format(firstNumber))
newNumber = int(input(' Number to add? '))
if newNumber + firstNumber == 10:
print("That worked!")
else:
print("That didn't work!")`
Upvotes: 2