• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Help with simple piece of code

LawGiver

2[H]4U
2FA
Joined
Oct 16, 2005
Messages
2,077
so im leerning python :) and was wondering why im getting this piece of code wrong. Whats the difference?

This is correct version:

import random
secret = random.randint(1, 99)
guess = 0
tries = 0
print "AHOY! I'm the Dread Pirate Roberts, and I have a secret!"
print "It is a number from 1 to 99. I'll give you 6 tries. "


while guess != secret and tries < 6:
guess = input("What's yer guess? ")
if guess < secret:
print "Too low, ye scurvy dog!"
elif guess > secret:
print "Too high, landlubber!"

tries = tries + 1

if guess == secret:
print "Avast! Ye got it! Found my secret, ye did!"
else:
print "No more guesses! Better luck next time, matey!"
print "The secret number was", secret


This is the incorrect version:

import random
secret = random.randint(1, 99)
guess = 0
tries = 0
print "AHOY! I'm Judge Dread, and I have a secret!"
print "It is a number from 1 to 99. I'll give you 6 tries"

while guess != secret and tries < 6:
guess = input("What's yer guess? ")
if guess < secret:
print "Too low, ye scurvy dog!"
elif guess > secret:
print "Too high, landlubber!"

tries = tries + 1

if guess == secret:
print "Avast! Ye got it! Found my secret, ye did!"
else:
print "No more guesses! Better luck next time matey!"
print "The secret number was", secret
 
you use a diff program to tell you the difference. I only see print text differences and an extra blank line
 
yeah i changed it to judge dread. but thats not the problem. The incorrect version acts like i guessed wrong six times even though I have only guessed once.
 
ok, it looks like identation defines blocks in python. You can't tell here because you inserted the examples in a quote block instead of a code block.

in the two examples the indentation is different. In the second example the indentation for if guess == secret makes it a part of the while block. See here:

6cksQnU.gif


fix the indentation and you will solve the problem
 
ug, python means living in perpetual whitespace hell. :)
 
ug, python means living in perpetual whitespace hell. :)

Because of that I refuse to ever use it without an IDE that punches you in the face with whitespace help. That's the worst design decision in any language I've ever used, I can't understand how Python became semi-popular... well, maybe I understand that most of it is because of lack of competition. Perl is pretty damn bad.
 
Back
Top