0

this script works in python2 but not in python3, the input question continues to be shown even if I put the correct answer :

correct = "no" while correct == "no": answer = input("15 x 17? ") if answer == 15*17: correct = "yes" print 'good!' #for python2 else: correct = "no" 

how this can be solved both not using and using a function?

0

2 Answers 2

1

In Python 3 input returns a string (in Python 2 input evaluates the input) so answer == 15*17 will never be true unless you convert answer to an int or 12*17 to string.

Also, print "good" is not a valid Python 3 syntax as print is a function: print("good").

Sign up to request clarification or add additional context in comments.

Comments

0

To make this work in both Py2.7 and Py3 there are couple of differences between the language versions - see What’s New In Python 3.0:

  • input() in py3 is equivalent to raw_input() in py2
  • print is no longer a statement in py3 but a function

To make this code work across both py2.7 and py3 you can do:

from __future__ import print_function try: input = raw_input except NameError: pass while True: answer = int(input("15 x 17? ")) if answer == 15*17: print('good!') break 

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.