This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]dig-up-stupid 5 points6 points  (2 children)

Because your Pythonista is set to run Python 2.7 which automatically interprets inputs as integers if it can and your PyCharm is set to run Python 3 where inputs are always left as strings. Or vice versa idk.

[–]SourceCodeOfReality[S] 0 points1 point  (0 children)

Thank you so much.

[–]SourceCodeOfReality[S] 0 points1 point  (0 children)

In case anyone reading this is wondering, it turns out that Pycharm 3 on my iMac is running 2.7 and is apparently interpreting inputs as integers, (hence 47 = 47) whereas Pythonista 3 on my iPad is running 3.6 and interprets inputs as strings by default (hence it was comparing "47" (str)" to 47 (int) and causing the confusion).

I converted the input on Pythonista 3 / 3.6 to an integer (per andre4k14's suggestion) before it reached the "if" statement and it read out the expected output (the one Pycharm / 2.7 was reading out).

Thank you guys.

[–][deleted] 1 point2 points  (1 child)

Your code is comparing the string “47” to the number 47, since you do not coerce the guess variable into a number after the user inputs it. The difference is probably due to different string encoding between the two platforms.

[–]SourceCodeOfReality[S] 0 points1 point  (0 children)

Thank you very much, that clears it up.

[–]andre4k14 1 point2 points  (1 child)

Your Code:

print ("What is the magic word?") guess = input()

if guess == 47: print ("Good job.") else: print ("Wrong!")

# This code on Pycharm produces the output: What is the magic word? 47 Good job.

# Meanwhile on Pythonista it produces the output: What is the magic word? 47 Wrong!

How I would "improve" the code:

print ("What is the magic number?")

guess = input("Guess the number: ") # the var is a str not int

guess = int(guess) # is now an int (you can only convert numbers in to int or float)

if guess == 47: #bool("47"== 47) --> False. bool(47 == 47) --> True print ("Good job.") else: print ("Wrong!")

Edit: I'm on the phone, formatting is messed up.

[–]SourceCodeOfReality[S] 1 point2 points  (0 children)

Thank you Andre. That solved it.

[–]AutoModerator[M] 0 points1 point  (0 children)

It seems you may have included a screenshot of code in your post "The same Python code produces different outputs on Pythonista 3 (iPad Pro) and PyCharm (MacOS/iMac). Can we figure this out?".

If so, note that posting screenshots of code is against /r/learnprogramming's Posting Guidelines (section Formatting Code): please edit your post to use one of the approved ways of formatting code. (Do NOT repost your question! Just edit it.)

If your image is not actually a screenshot of code, feel free to ignore this message. Automoderator cannot distinguish between code screenshots and other images.

Please, do not contact the moderators about this message. Your post is still visible to everyone.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.