• 0

Python Problem


Question

I'm back. Still chugging away on my Network Admin degree and I have to take this Python Programming class. Well, It takes a special, very smart person to be a programmer, which I could never be.

Anyhoo, I've got an assignment here that I'm stuck on. My flowchart is logical, I know what to do, but my code is off.

I'm supposed to create a program that presents a person running it with a random fortune, however, I'm getting a syntax error around my first if/then statement.

Here's what I got

import random

n = random.ranrange(5) + 1

if n = 1:

print Be prepared

if n = 2:

print Never give advice unless asked

if n = 3:

print Behind an able man are always other able men

if n = 4:

print Complain tot he one who can help you

if n = 5:

print All sins cast long shadows

raw_input("\n\nPress the enter key to exit.")

Please let me know what I'm doing wrong and how to correct it.

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0
I know nothing about Python, but maybe there should be "if n == 1"

like in C/Java/...

n = 1 n is assigned the value of one

n == 1 n is equal to one

Well, I tried it and it worked. I also noticed that my print statements did not have quotes which was a problem, so I fixed it and it worked. Thanks for the info!

One more question for anyone who knows. How do I add an if/then to this program where if a person guesses at least 10 times, a message comes back to him, but he can keep guessing until he figures it out? I've included the area where the statement should be (in Bold), but after 10 tries it's not showing up. I think I'm off somewhere where I haven't define "tries" properly.

import random

print "\tWelcome to 'Guess the Number or be ridiculed'!"

print "\nI'm thinking of a number between 1 and 25."

print "Try to guess it in 10 attempts"

a_number = random.randrange(25) + 1

guess = int(raw_input("Take a guess: "))

tries = 1

while (guess != a_number):

if (guess > a_number):

print "The number is lower..."

else:

print "The number is higher..."

guess = int(raw_input("Take another guess: "))

tries += 1

if tries >= 10:

print "My 6 month old can guess in 10 tries. What's wrong with you?"

print "You guessed it! The number was", a_number

print "And it only took you", tries, "tries!\n"

raw_input("\n\nPress the enter key to exit.")

Link to comment
Share on other sites

  • 0

I installed Python! :D

I copyed your code and indented it. It works exactly like you wanted!

(maybe you have indented the code a little wrong...)

Here's your code indented:

import random
print "\tWelcome to 'Guess the Number or be ridiculed'!"
print "\nI'm thinking of a number between 1 and 25."
print "Try to guess it in 10 attempts"

a_number = random.randrange(25) + 1
guess = int(raw_input("Take a guess: "))
tries = 1

while (guess != a_number):
  if (guess > a_number):
    print "The number is lower..."
  else:
    print "The number is higher..."

  guess = int(raw_input("Take another guess: "))
  tries += 1

  if tries >= 10:
    print "My 6 month old can guess in 10 tries. What's wrong with you?"

print "You guessed it! The number was", a_number
print "And it only took you", tries, "tries!\n"


raw_input("\n\nPress the enter key to exit.")

Link to comment
Share on other sites

  • 0

Why is indenting a big thing? For that code, I created a loop last night because the indenting was off?

I don't get programming and probably never will. Thank God we have you guys to brave this frontier!!!! :D

Thanks mikulka, I'll probably see you next week with more questions! ;)

Link to comment
Share on other sites

  • 0

Indenting is important because it shows which commands belong inside "if" (or something else) and which commands don't belong inside that "if".

I'm glad I could help :happy:

I'm really starting to like Python.

Thank you for introducing me to Python!

Link to comment
Share on other sites

  • 0

Python is indeed great (made by a Dutchman :whistle: ). Unlike other programming languages, if you don't indent correctly, you're program will not function like you want it to. You can prevent most problems by using a good IDE (Integrated Development Enviroment) though.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.