webdevRefinery Forum: Else/If Help - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline Koregg 

  • Group: Members
  • Posts: 23
  • Joined: 31-December 11

Posted 11 August 2012 - 09:31 AM (#1)

Else/If Help


Hi, I have recently started following some python tutorials and started this mini project, I asked my friend who did a bit of Python before but this as far as he could get me. No matter what I enter in the Yes/No segment, it leads me to "You must be an idiot." Not sure why, would anyone care to help? The code is here: http://pastebin.com/Bpi3rthm
Thanks!
0


User is offline NeilHanlon 

  • Group: Members
  • Posts: 884
  • Joined: 08-July 10
  • LocationRowley, Massachusetts
  • Expertise:HTML,CSS,PHP,Java,Graphics

Posted 11 August 2012 - 09:41 AM (#2)

If I had to guess (Since I don't know python), it'd be that you might have to convert the raw input to a string/integer or something.
Thanks,
兄ニール

Website | Blog | @NeilHanlon | About.Me | Facebook | LinkedIn
0


User is offline Koregg 

  • Group: Members
  • Posts: 23
  • Joined: 31-December 11

Posted 11 August 2012 - 09:45 AM (#3)

Yeah I thought so too, but I tried it and couldn't get it to work :/ Thanks though!
0


User is offline Sephern 

  • Group: Moderators
  • Posts: 967
  • Joined: 04-June 10
  • LocationReading, UK
  • Expertise:HTML,CSS,PHP,Javascript,Python

Posted 11 August 2012 - 10:10 AM (#4)

The issue is that you're comparing things of a different type.
raw_input
gives back a string, so you're comparing a string to an integer. That'll always return false.

In essence what you're doing is
"9" == 9


If you run that past a python interpreter you'll find that it's always false.

You have 2 options. The first is that you could convert your input into an integer. The issue with this is that it'll throw an exception if it can't convert properly (i.e. if I put "hello" into your input, there's no way of making that a number). You could catch this exception and handle it appropriately (in fact, that's probably what you'd do in most situations) but here it seems that you'd just want normal strings to be handled like any other invalid input.

You could, therefore, turn the ints on the right hand side of your comparison into string literals instead.

if yn == "9":

0


User is offline NeilHanlon 

  • Group: Members
  • Posts: 884
  • Joined: 08-July 10
  • LocationRowley, Massachusetts
  • Expertise:HTML,CSS,PHP,Java,Graphics

Posted 11 August 2012 - 10:23 AM (#5)

View PostSephern, on 11 August 2012 - 10:10 AM, said:

The issue is that you're comparing things of a different type.
raw_input
gives back a string, so you're comparing a string to an integer. That'll always return false.

In essence what you're doing is
"9" == 9


If you run that past a python interpreter you'll find that it's always false.

You have 2 options. The first is that you could convert your input into an integer. The issue with this is that it'll throw an exception if it can't convert properly (i.e. if I put "hello" into your input, there's no way of making that a number). You could catch this exception and handle it appropriately (in fact, that's probably what you'd do in most situations) but here it seems that you'd just want normal strings to be handled like any other invalid input.

You could, therefore, turn the ints on the right hand side of your comparison into string literals instead

if yn == "9":




Purely as a learning experience, does Python not have the same difference most languages have between == and === (Where == doesn't check type, and === does)?
Thanks,
兄ニール

Website | Blog | @NeilHanlon | About.Me | Facebook | LinkedIn
0


User is offline Sephern 

  • Group: Moderators
  • Posts: 967
  • Joined: 04-June 10
  • LocationReading, UK
  • Expertise:HTML,CSS,PHP,Javascript,Python

Posted 11 August 2012 - 10:41 AM (#6)

View PostNeilHanlon, on 11 August 2012 - 10:23 AM, said:

Purely as a learning experience, does Python not have the same difference most languages have between == and === (Where == doesn't check type, and === does)?

No. Python is a strongly typed language, so you have to cast/convert between types (it'd make no sense to have a triple equals).
0


User is offline callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 11 August 2012 - 10:54 AM (#7)

The double equals operator in loosely typed languages is a silly operator: it promotes excessive laziness and can create some just plain weird behaviour.
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is offline Ruku 

  • I do Linux and that Internet thing.
  • Group: Members
  • Posts: 1351
  • Joined: 17-April 10
  • Location/root
  • Expertise:HTML,CSS,PHP,Javascript,Python,SQL

Posted 11 August 2012 - 01:30 PM (#8)

View Postcallumacrae, on 11 August 2012 - 10:54 AM, said:

The double equals operator in loosely typed languages is a silly operator: it promotes excessive laziness and can create some just plain weird behaviour.

I've never actually given this much thought, but I agree on the grounds that it's easier to read code where the type conversions are explicit. That said, weird behaviour tends to stem from misunderstanding, especially amongst new and inexperienced programmers. I very rarely see
===
appear in even the most established, commercially-backed PHP projects, for instance.
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 11 August 2012 - 01:42 PM (#9)

the
===
is equal to
&one == &two
in terms of C right? In ruby you can compare object ids instead, what would be the PHP alt? Or better yet, Python alt.
Reserved.
0


User is online Lemon 

  • I have a dream...
  • Group: Members
  • Posts: 688
  • Joined: 24-February 11
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 11 August 2012 - 02:03 PM (#10)

View PostTheEmpty, on 11 August 2012 - 01:42 PM, said:

the
===
is equal to
&one == &two
in terms of C right? In ruby you can compare object ids instead, what would be the PHP alt? Or better yet, Python alt.

Not quite, as they could be different addresses in memory but they just to have the same value and type; for == you just need same casted value but not necessarily the same type. If you're thinking in terms of C, === would be like an equality in which there is no cast (i.e same types) but the pointers are not necessarily the same. From the PHP manual:

$a == $b -> "TRUE if $a is equal to $b after type juggling."
$a === $b -> "TRUE if $a is equal to $b, and they are of the same type."

Edit: Oh, Python topic so this is not really particularly useful <_<
Posted Image
In the end, it's not the years in your life that count. It's the life in your years
3


User is offline lobabob 

  • Group: Members
  • Posts: 98
  • Joined: 21-March 10
  • LocationWouldn't you like to know?
  • Expertise:HTML,CSS

Posted 11 August 2012 - 03:27 PM (#11)

Quote

Not quite, as they could be different addresses in memory but they just to have the same value and type; for == you just need same casted value but not necessarily the same type. If you're thinking in terms of C, === would be like an equality in which there is no cast (i.e same types) but the pointers are not necessarily the same. From the PHP manual:

$a == $b -> "TRUE if $a is equal to $b after type juggling."
$a === $b -> "TRUE if $a is equal to $b, and they are of the same type."


:o .........I finally know what === means
"When the world goes mad, one must accept madness as sanity since sanity is, in the last analysis, nothing but the madness on which the whole world happens to agree." - George Bernard Shaw

Life = http://xkcd.com
0


User is offline TheMaster 

  • *-c0de master-*
  • Group: Members
  • Posts: 748
  • Joined: 24-May 10
  • LocationAustralia
  • Expertise:HTML,CSS,PHP,Java

Posted 11 August 2012 - 11:06 PM (#12)

View PostLemon, on 11 August 2012 - 02:03 PM, said:

$a == $b -> "TRUE if $a is equal to $b after type juggling."
$a === $b -> "TRUE if $a is equal to $b, and they are of the same type."


This should be put in programming guides from now on :P
0


User is offline callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 12 August 2012 - 05:32 AM (#13)

It has too many crazy side effects.

"032" == "32"
"032" == 32
032 != 32

"\n \t \r" == 0

Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is online Lemon 

  • I have a dream...
  • Group: Members
  • Posts: 688
  • Joined: 24-February 11
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 12 August 2012 - 03:10 PM (#14)

View PostTheMaster, on 11 August 2012 - 11:06 PM, said:

This should be put in programming guides from now on :P

It already is, I just took that from the php manual.
Posted Image
In the end, it's not the years in your life that count. It's the life in your years
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services