Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

python- add 1 to variable, conditional


  • Please log in to reply

#1
TaNkZ101

TaNkZ101

    Member

  • Member
  • PipPipPip
  • 327 posts
I want to make a little simple quiz in python. how would i tell it:
if answer = "b":
"add 1 to correct_answers"
else:
"add 1 to incorrect_answers"
.
also, i want the user to be able to quit the quiz at any time (by typing, for example, .quit). how would i tell python that? maybe i'm getting ahead of myself. i want the quiz to be in the style of:
if q_1 = "a":
"add 1 to correct_answers" AND(how to do this?) print "You have answered correctly"
else:
"add 1 to incorrect_answers" AND print "sorry, the correct answer was", a.

just one more q how exactly do i make a line break?
  • 0

Advertisements


#2
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
import sys

correct_answers = 0
incorrect_answers = 0

answer = raw_input("input answer: ")
if answer == ".quit":
	 sys.exit()
elif answer == "b":
	 correct_answers++
	 print "You have answered correctly"
else:
	 incorrect_answers++
	 print "Sorry, the answer was b"

line breaks are created by default whenever you write a 'print' statement. If you do NOT want a newline at the end of your output, add a comma (,) to the end of the print command, e.g.:

print "this line will keep going",

if you mean you just want to output a blank line, just do:

print

Hope this helps :whistling:

Edited by Swandog46, 27 July 2006 - 08:51 AM.

  • 0

#3
TaNkZ101

TaNkZ101

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 327 posts
thanks it did a lot. so this means i have to make an if...elseif...else statement for each q? i can't put something just at the beginning of the program that would work the whole quiz? (i guess not since code has to be executed line by line?). anyway u have definately helped me!, now i can start. one more general question about the python language: (how) can i make a python script into an executable program (exe)? i'd like to make this .py (when i finish it) into an .exe, and that when you open it, it opens, idk, the dos command promt, or something like that. is there something big im missing out on about the language?
edit: i read in more than one place that python is "interprated" rather than "compiled". does this mean i cannot make an exe from it? is there another way to do what i want?

Edited by TaNkZ101, 27 July 2006 - 03:51 PM.

  • 0

#4
TaNkZ101

TaNkZ101

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 327 posts
on another not: i get an invalid syntax problem, and the red starts after "correct_answers++"
  • 0

#5
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP

i have to make an if...elseif...else statement for each q?

You could --- there is probably a more elegant solution if you would like to look for one. I might try something like making an array of the correct answer choices and looping over it.

edit: i read in more than one place that python is "interprated" rather than "compiled".

Yes, that's right.
http://en.wikipedia....preted_language

does this mean i cannot make an exe from it?

Not natively, but there are libraries to do it for you. Notably:
http://www.py2exe.org/

on another not: i get an invalid syntax problem, and the red starts after "correct_answers++"

Oops! :whistling:
I forgot that Python doesn't have the ++ syntax. Try this instead:

correct_answers = correct_answers + 1
[snip]
incorrect_answers = incorrect_answers + 1
  • 0

#6
TaNkZ101

TaNkZ101

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 327 posts
ok ok thx. if i wasn't going to get any help/replies i'd have tried the variable equals variable plus one anyway. i appreciate all the help your offering. i have to read up on what an array is in a little more detail now. if you could make an example for me, could u just please supply it with comments?
  • 0

#7
TaNkZ101

TaNkZ101

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 327 posts
i've noticed after the program executes its last line of code, it closes almost instantly. can i somehow put a time-delay of 3 seconds between executing the last line and ending?
  • 0

#8
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
The creator of the language can explain it better than I can. Read the Python tutorial here:
http://docs.python.org/tut/tut.html

It has sections on data structures such as lists that are built into the language.

can i somehow put a time-delay of 3 seconds between executing the last line and ending?


Yup.

from time import sleep
sleep(3)

  • 0

#9
TaNkZ101

TaNkZ101

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 327 posts
cool. im so happy python is my first programming language (except php if u count it). syntax extremely simple, no excessive brackets.
  • 0

#10
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
Yes, it is a beautifully simple language. PHP is great too for website work, but Python is more versatile.

My two (and only two) gripes about Python:
1) It doesn't have the ++ syntax I tried to use two posts ago (every other sensible language does)
2) It doesn't have a simple "C-style" for loop. Its 'for' is like Perl's 'foreach'. Why didn't they just make 'for' be 'for' and 'foreach' be 'foreach'?? :whistling:

Edited by Swandog46, 28 July 2006 - 10:52 AM.

  • 0

#11
TaNkZ101

TaNkZ101

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 327 posts
i read on their website that instead of ++ to use +=, -= respectively
  • 0

#12
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
Oh really? If it has +=, why not ++ ?? grrrr

OK, so basically these expressions:

my_var++
my_var += 1

are both shorthand in most programming languages for:

my_var = my_var + 1

It will just increment my_var by 1. I guess Python has += but not ++.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP