Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python help thread?
#1
Does anyone here code in Python? I for serious need some help with a number guessing game.

"In this assignment, you'll create a program that plays a simple game. The computer will generate a random integer between 1 and 20, and the user has up to three chances to guess the correct number. If the user guesses the correct number, they are informed of that. If they use all three guesses without guessing the correct number, they are told what the correct number is.

Here's a couple of examples of how the game might look (computer in blue, player in black):

1st try. Player correctly guesses the number."

Here is what I have so far:

 Spoiler
I think that is correct...
Reply
#2
Lay on your back, with your arms and legs pointed straight. And wait for a corporate executive, Fiel, to come and assist you.
Reply
#3
This is in the funhouse because...
Reply
#4
This part is incorrect: "if n != randint(1,20)" What you're doing here is comparing n which is what they've entered to an random number. You should compare this to "num" which you generated at the beginning of the script.

Also it looks like you've only asked the user for the number ONCE but "tried" to evaluate it 3 times with if statements incorrectly.


This is what I think that would work...

from random import randint

num = randint(1,20)

for i in range(1, 4):
def zenumbers(n):
if n != num
print "Guess again!"
else
print "you got it!"
break
else:
print "Used up all 3 guesses"
Reply
#5
Oh, sorry about that. I'm a newb, go easy on me. Python is spam to me.
Reply
#6
o-o I don't know what you're sorry about XD, you asked for help. Try the code I presented and see if it works. I don't know python either and I don't have it installed.
Reply
#7
XTOTHEL Wrote:o-o I don't know what you're sorry about XD, you asked for help. Try the code I presented and see if it works. I don't know python either and I don't have it installed.

No, doesn't work, but thanks for the help! I might just have to break down and actually read this teachers horrible explanations.
Reply
#8
Alley Wrote:No, doesn't work, but thanks for the help! I might just have to break down and actually read this teachers horrible explanations.


now?
Code:
from random import randint

rnum = randint(1,20)

for i in range(1, 4):
    guess = input("Guess a number")
        if guess != rnum:
            print "Guess again!"
        else:
            print "You got it!"
            break
else:
    print "Used up all 3 guesses"
Reply
#9
XTOTHEL Wrote:now?
Code:
from random import randint

rnum = randint(1,20)

for i in range(1, 4):
    guess = input("Guess a number")
        if guess != rnum:
            print "Guess again!"
        else:
            print "You got it!"
            break
else:
    print "Used up all 3 guesses"

You can't have 2 else statements in a definition. Its okay, I'll just copy it from a friend or something.. Thanks for trying. Heart
Reply
#10
Alley Wrote:You can't have 2 else statements in a definition. Its okay, I'll just copy it from a friend or something.. Thanks for trying. Heart

I swear this should work:

Code:
from random import randint

rnum = randint(1,20)
print rnum

for i in range(1, 4):
        guess = input("Guess a number : ")
        
        if guess == rnum:
                print "You got it!"
                break
        else:
                if i < 3:
                        print "Guess again!"
else:
        print "Used up all 3 guesses"
Reply
#11
XTOTHEL Wrote:I swear this should work:

It does.
the only thing that could've gone wrong is if the indention was copied incorrectly.
It's not having what you want - It's wanting what you've got.
Reply
#12
Eosian Wrote:It does.
the only thing that could've gone wrong is if the indention was copied incorrectly.

Yea, who knew indentation was so important Rolleyes
Reply
#13
XTOTHEL Wrote:Yea, who knew indentation was so important Rolleyes

Anyone who so much as skimmed the Wikipedia opening paragraph on Python Rolleyes
It's not having what you want - It's wanting what you've got.
Reply
#14
Eosian Wrote:Anyone who so much as skimmed the Wikipedia opening paragraph on Python Rolleyes

I headed straight for the docs.
Reply
#15
XTOTHEL Wrote:Yea, who knew indentation was so important Rolleyes

Spending hours getting error messages only to find out that your code was perfect, but you had a secret little indentation. I will swear by this, Python hates gingers. Frown

@Your code. It worked! I did one *major*Rolleyes tweak. The print rnum at the beginning was displaying the secret number.
Reply
#16
Alley Wrote:@Your code. It worked! I did one *major*Rolleyes tweak. The print rnum at the beginning was displaying the secret number.

That was for debugging purposes so you could tell if it was working correctly. It was never meant to be left in once you validated it did what you wanted.
It's not having what you want - It's wanting what you've got.
Reply
#17
This makes me hot. please continue.
Reply
#18
Alley Wrote:You can't have 2 else statements in a definition. Its okay, I'll just copy it from a friend or something.. Thanks for trying. Heart

Confusing, but an else statement after a for loop in Python, is syntactic sugar for lines that should execute if and only if the loop executed without break-ing.
Reply
#19
Can someone quickly double check this:

Assumptions: Only valid number inputs for the range. That is, no floating point values, letters and the like.

Result that comes out of this code should be exactly the same as this:

Code:
Do you want to play ('y' for yes)? y
Enter the low limit on the number: 200
Enter the high limit on the number: 500
Enter the maximum number of guesses: 4
I will never need more than 9 guesses.
I guess 350 am I low, correct, or high? high
I guess 274 am I low, correct, or high? high
I guess 236 am I low, correct, or high? low
I guess 255 am I low, correct, or high? high
You win, I loose!
Do you want to play ('y' for yes)? y
Enter the low limit on the number: 1
Enter the high limit on the number: 10
Enter the maximum number of guesses: 7
I will never need more than 4 guesses.
I guess 5 am I low, correct, or high? high
I guess 2 am I low, correct, or high? high
I guess 1 am I low, correct, or high? high
No way, you cheated!
Do you want to play ('y' for yes)? y
Enter the low limit on the number: 200
Enter the high limit on the number: 300
Enter the maximum number of guesses: 7
I will never need more than 7 guesses.
I guess 250 am I low, correct, or high? high
I guess 224 am I low, correct, or high? low
I guess 237 am I low, correct, or high? high
I guess 230 am I low, correct, or high? low
I guess 233 am I low, correct, or high? high
I guess 231 am I low, correct, or high? correct
I win!
Do you want to play ('y' for yes)? y
Enter the low limit on the number: 200
Enter the high limit on the number: 230
Enter the maximum number of guesses: 5
I will never need more than 5 guesses.
I guess 215 am I low, correct, or high? gihd
I don't understand gihd
I guess 215 am I low, correct, or high? high
I guess 207 am I low, correct, or high? loow
I don't understand loow
I guess 207 am I low, correct, or high? low
I guess 211 am I low, correct, or high? hihg
I don't understand hihg
I guess 211 am I low, correct, or high? high
I guess 209 am I low, correct, or high? low
I guess 210 am I low, correct, or high? correct
I win!
Do you want to play ('y' for yes)? n

Code:
import random

def high_low(low_range, high_range, NumberGuess):
    guess = 0
    flag = 0
    RandNum = random.randint(low_range, high_range)
    while flag == 0:
        while guess < NumberGuess and flag == 0:
            Answer = raw_input('I guess '+ str(RandNum)+ ' am I low, correct, or high? ')
            if Answer in ('c','C','correct','Correct'):
                print 'I win!'
                flag = 1
            elif Answer in ('h','H','High','high'):
                high_range = RandNum-1
                Cheat(low_range, high_range+1)
                RandNum = random.randint(low_range, high_range)
                guess = guess + 1
            elif Answer in ('l','L','Low','low'):
                low_range = RandNum+1
                Cheat(low_range-1, high_range)
                RandNum = random.randint(low_range, high_range)
                guess = guess + 1
            else:
                print 'I do not understand', Answer+'.'
        if guess == NumberGuess and flag == 0:
            print 'You win, I lose!'
            flag = 1
    Start_game()

def Start_game():
    Start = raw_input('Do you want to play ("y" for yes)? ')
    if Start in ('y','yes','Y','Yes'):
        low_range = int(raw_input('Enter the low limit on the number: '))
        high_range = int(raw_input('Enter the high limit on the number: '))
        while low_range > high_range:
            print 'How do you have a range of the low limit being higher than',
            print 'the high limit?'
            print 'Please give me a valid range.'
            low_range = int(raw_input('Enter the low limit on the number: '))
            high_range = int(raw_input('Enter the high limit on the number: '))
        while low_range == high_range:
            print 'Uh, that is not a range.  Please give a range so we can',
            print 'start playing!'
            low_range = int(raw_input('Enter the low limit on the number: '))
            high_range = int(raw_input('Enter the high limit on the number: '))
        NumberGuess = int(raw_input('Enter the maximum number of guesses: '))
        if NumberGuess < 0:
            print 'That was a negative number of guesses. I am sure you meant',
            print 'the opposite so that is', NumberGuess*(-1), 'guesses.'
            NumberGuess = NumberGuess*-1
        while NumberGuess == 0:
            print "I don't get any guesses? :("
            NumberGuess = int(raw_input('Enter the maximum number of guesses: '))
        if NumberGuess == 1:
            print 'I will never need more than', NumberGuess, 'guess.'
        else:
            print 'I will never need more than', NumberGuess, 'guesses.'
        high_low(low_range, high_range, NumberGuess)
    else:
        exit()

def Cheat(low_range, high_range):
    if low_range >= high_range:
        print 'No way! You cheated!'
        Start_game()
        
Start_game()

I did add some of my own stuff in there for additional dummy proofing. But it shouldn't change the output result should you enter in the same stuff as the required input.
Reply
#20
Okay, so I have a new assignment in class to write a program that translates a word into pig latin. From what I've seen by the examples, a word beginning with 2 consonants should be translated up to the first vowel and have the consonants that were removed appear in front of the "ay" but I have no clue how to get past the first. Any ideas?

Code:
from string import *

word = raw_input("Enter the word you would like to have translated: ")
if word[0].lower == "a" or word[0].lower == "e" or word[0].lower == "i" or word[0].lower == "o" or word[0].lower == "u":
    word = word + "-" + "ay"
    print word
elif word[0].lower == "y":
    if word[1] != "a" and word[1] != "e" and word[1] != "i" and word[1] != "o" and word[1] != "u":
        word = word + "-" + "ay"
        print word
    elif word[1] == "a" or word[1] == "e" or word[1] == "i" or word[1] == "o" or word[1] == "u":
        word = word[1:] + "-" + word[0] + "ay"
        print word
elif word[0].lower != "a" or word[0].lower != "e" or word[0].lower != "i" or word[0].lower != "o" or word[0].lower != "u":
        word = word[0:] + "-" + word[0] + "ay"
        print word

Also @above:
Why do you use int(raw_input())? input() works just the same as that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)