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?

2 Answers2

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").

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
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
AChampion
  • 29,683
  • 4
  • 59
  • 75