Jump to content

Photo

Error in my simple script


  • Please log in to reply
16 replies to this topic

#1
JCJeff

JCJeff
  • Members
  • 55 posts
  • Joined: 18-April 12
  • LocationCanada
  • Expertise:Python
I'm currently reading Hello World-Computer Programming for kids or other beginners

I'm trying to solve this question:

4 What kind of if statement would you use to check if the user entered the letter “Q” in
either uppercase or lowercase?


ins = raw_input("Type Q in lower or uppercase: "
if ins = "Q"
    print "You typed Q"
if ins = "q"
    print "you typed q"

There's an error here but what is it? :unsure:

#2
Qasim

Qasim
  • Members
  • 548 posts
  • Joined: 25-October 10
  • LocationOntario, Canada
  • Expertise:HTML, CSS, PHP, Java, Javascript, Python, Ruby on Rails, Node.js, Graphics, MongoDB, Cassandra
Does
if ins == "q":
    print "lowercase"
elif ins == "Q":
    print "uppercase"
work for you?

#3
JCJeff

JCJeff
  • Members
  • 55 posts
  • Joined: 18-April 12
  • LocationCanada
  • Expertise:Python

Does

if ins == "q":
    print "lowercase"
elif ins == "Q":
    print "uppercase"
work for you?

no, syntax error on
print


#4
ianonavy

ianonavy
  • Members
  • 921 posts
  • Joined: 14-April 10
  • Expertise:HTML, CSS, Java, Javascript, Python, SQL
You forgot to close the parenthesis from
raw_input
, you need to use two equal signs (
==
) instead of one equal sign for comparisons, and you need colons after your if statements.

#5
JCJeff

JCJeff
  • Members
  • 55 posts
  • Joined: 18-April 12
  • LocationCanada
  • Expertise:Python

You forgot to close the parenthesis from

raw_input
, you need to use two equal signs (
==
) instead of one equal sign for comparisons, and you need colons after your if statements.

oh, thanks... Silly me

#6
ianonavy

ianonavy
  • Members
  • 921 posts
  • Joined: 14-April 10
  • Expertise:HTML, CSS, Java, Javascript, Python, SQL
It should look like this:
ins = raw_input("Type Q in lower or uppercase: ")
if ins == "Q":
    print "You typed Q"
elif ins == "q":
    print "you typed q"

Note that I changed
if
to
elif
, which is short for "else if". Basically, it only checks if the input is equal to "q" if the comparison to "Q" is
False
.

oh, thanks... Silly me


No worries. :) Syntax errors are common when programming, but they become less frequent as you become more experienced. What you have to watch out for are logic errors. ;)

Edit: LOL, it took me several months to realize what Quinn said.

Edited by ianonavy, 16 August 2012 - 03:00 PM.


#7
Quinn

Quinn

    More pew-pew, less QQ

  • Members
  • 1,307 posts
  • Joined: 08-March 10
  • LocationPalmyra, PA, USA
  • Expertise:HTML, PHP, Javascript

It should look like this:

ins = raw_input("Type Q in lower or uppercase: ")
if ins = "Q":
    print "You typed Q"
elif ins = "q":
    print "you typed q"

Note that I changed
if
to
elif
, which is short for "else if". Basically, it only checks if the input is equal to "q" if the comparison to "Q" is
False
.



No worries. :) Syntax errors are common when programming, but they become less frequent as you become more experienced. What you have to watch out for are logic errors. ;)

ins = raw_input("Type Q in lower or uppercase: ")
if ins == "Q":
    print "You typed Q"
elif ins == "q":
    print "you typed q"
You made the same mistake that you pointed out.
<Imp> [F3ar 40]  [PWNbear 17]  [magik 15]  [dissident 10]  [mark 7]

Though anyone who thinks Europe is a country should be smacked in the face. By a train.


#8
JCJeff

JCJeff
  • Members
  • 55 posts
  • Joined: 18-April 12
  • LocationCanada
  • Expertise:Python
Thanks

#9
NeilHanlon

NeilHanlon
  • Members
  • 1,035 posts
  • Joined: 08-July 10
  • LocationRowley, Massachusetts
  • Expertise:HTML, CSS, PHP, Java, Graphics
That one should be:

purchases = int(raw_input("How much does it cost? "))
print "You have", purchases, "dollars"
if purchases <= 10:
    print "You have a 10% discount"
    print "Your total is: ", purchases * 0.10 + purchases, "dollars"
elif purchases > 10:
    print "You have a 20% discount"
    print "Your total is: ", purchases * 0.20 + purchases, "dollars"

Notice it's <= 10, not =< 10
Thanks,
兄ニール


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


#10
JCJeff

JCJeff
  • Members
  • 55 posts
  • Joined: 18-April 12
  • LocationCanada
  • Expertise:Python

That one should be:

purchases = int(raw_input("How much does it cost? "))
print "You have", purchases, "dollars"
if purchases <= 10:
    print "You have a 10% discount"
    print "Your total is: ", purchases * 0.10 + purchases, "dollars"
elif purchases > 10:
    print "You have a 20% discount"
    print "Your total is: ", purchases * 0.20 + purchases, "dollars"

Notice it's <= 10, not =< 10

I noticed that myself after checking it over! I edited when you were replying

#11
NeilHanlon

NeilHanlon
  • Members
  • 1,035 posts
  • Joined: 08-July 10
  • LocationRowley, Massachusetts
  • Expertise:HTML, CSS, PHP, Java, Graphics

I noticed that myself after checking it over! I edited when you were replying


Aha. :P
Thanks,
兄ニール


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


#12
JCJeff

JCJeff
  • Members
  • 55 posts
  • Joined: 18-April 12
  • LocationCanada
  • Expertise:Python

Aha. :P

Thanks anyways :lol:

#13
DisneyRicky

DisneyRicky
  • Members
  • 58 posts
  • Joined: 17-June 10
  • LocationCalifornia
  • Expertise:HTML, CSS, Graphics
Since you've made it this far, you should really start working on Python3.
That's what I am teaching myself right now.

#14
Signify

Signify
  • Members
  • 14 posts
  • Joined: 28-March 13
  • Expertise:HTML, CSS, Javascript

Since you've made it this far, you should really start working on Python3.
That's what I am teaching myself right now.

I dunno. I've heard talk of a lot of developers refusing to make the move. Depending on what you plan on doing, you may get better support on 2.7 unless you are going to make self-running packages in Windows. I don't believe Ubuntu even supports 3 by default. Seems like it would be a bigger hassle for your clients if you plan to have them.

I got far enough into Python to start working on GUIs. Sadly, I had no idea what to do when I got to how it handled its OOP. Confused me to death. I haven't touched it in a few months.
I'm a newbie. If you come across any helpful tutorial in Java, Python, CSS3, HTML5, or canvas and have a few seconds, sharing it would be greatly appreciated. I'm still just getting started and am looking to get off the ground to become more experienced in web development.

#15
JeremyCar

JeremyCar
  • Members
  • 2 posts
  • Joined: 14-December 14
You are passing two resolutionIndex values through the the second and resolutionIndex = 0 parameters causing the error. Use getAIN like this to set the resolution index:-labjackU6.getAIN0, resolutionIndex = 0, differential = Trueor-labjackU6.getAIN0, 0, differential = True

#16
Hyde

Hyde
  • Members
  • 1,825 posts
  • Joined: 08-March 10
wtf

#17
SapporoGuy

SapporoGuy
  • Members
  • 1,970 posts
  • Joined: 10-June 11
  • LocationSapporo Beer Factory
It's Krishna coding ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users