0

I am wanting to use str.format() with the variable randomOne, since %s doesn't seem to be working for me:

print('The number generated between %s and %s is' + number) % (randomOne, randomTwo)

And I get this error:

TypeError: unsupported operand type(s) for %: 'nonetype' and 'tuple'
1
  • You have three separate problems here: First, str.format doesn't use %s placeholders, it uses {} placeholders. Second, you're not actually calling str.format, you're using the % operator. And finally, you're using the operator on the result of print-ing the string, rather than on the string you wanted to print. @sweeneyrod's answer fixes all three (in two different ways, depending on whether you actually wanted str.format as you said, or % as your code showed). Commented Nov 27, 2013 at 20:02

2 Answers 2

1

You need to do the formatting operation on the string, not the operation of printing the string. That is the cause of your current problem. If you want to use str.format in this case you would do this:

print('The number generated between {} and {} is {}'.format(randomOne, randomTwo, number))

In the event you want to stick with old style string formatting (which I wouldn't advice), you would do this:

print('The number generated between %s and %s is %s' % (randomOne, randomTwo, number))
Sign up to request clarification or add additional context in comments.

Comments

1

You just have to use the brackets at the right place

print('The number generated between %s and %s is %d' % (randomOne, randomTwo, number))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.