Hi there, I know there are some changes between Python 2x and 3x, and I know this is probably due to the changes with how 3x handles unicode and binary but I dont know how to make it work properly in 3x.

In 3x my code returns the value I enter, in 2x the code works properly and returns MOO or Cluck Cluck! properly when i select 1 or 2. How do I get this to work in 3x as well?

import os

def question():
	os.system('CLS')
	print ("Make your selection\n\n1 - Cows\n2 - Chickens\n")
	answer = input("> ")
	return answer

def cluck():
	print ("cluck cluck cluck")
	
def moo():
	print ("mooooooooooooOOO")

x = question()

if x == 1:
	moo() 
elif x == 2:
	cluck()
else:
	print(x)

Recommended Answers

All 2 Replies

The problem in python 3 code is that you are comparing integer with string. input() in python 3 return a string same as raw_input() in python 2. input() in python 2 return an integer.
This will work

answer = int(input("> ")) #make input return an integer

Or you can cast it to an integer later.

if int(x) == 1:

Read about the differnce.
http://diveintopython3.org/porting-code-to-python-3-with-2to3.html

Use 4 space as indentation in your code,not 8.

commented: nice +13

input's meaning is same as before raw_input in Python 2. This is good thing, as it is not good idea to ever use input in Python 2. I myself usually do this like in the beginning of this simple program (split emulator to demonstrate itertools.groupby) to get programs run in both Python 3 and 2 and to learn the latest ways to do things:

from __future__ import print_function, division
try:
    input = raw_input
    range = xrange
except:
    pass

from itertools import groupby

def isspace(x):
    return x==' '

sometext = input("Type something: ")
print('['+
      ', '.join(repr(''.join(word))
                     for spaces, word in groupby(sometext, isspace)
                     if not spaces)+
      ']')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.