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():
	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:
elif x == 2:

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.

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

Edited 5 Years Ago by snippsat: n/a


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
    input = raw_input
    range = xrange

from itertools import groupby

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

sometext = input("Type something: ")
      ', '.join(repr(''.join(word))
                     for spaces, word in groupby(sometext, isspace)
                     if not spaces)+

Edited 5 Years Ago by pyTony: n/a

This question has already been answered. Start a new discussion instead.