A few simple(I think) python3 problems

Chat about just about anything else

A few simple(I think) python3 problems

Postby RETNUH on Mon Dec 12, 2011 8:02 pm

Ok so i'm making a game of 21 in python3 to be played in the terminal. However I am running into a few problems.

here is the game so far
Code: Select all
import random
print("Try to get as close to 21 as you can without going over")
a = random.randint(0, 12)
b = random.randint(0,  12)
print("Your current total is ", a + b)
total = a + b
hit = input("Hit? y or n")
while total < 21:
    if hit == 'y':
        c = random.randint(0, 12)
        total2 = total + c
        print("your new total is ", total2)
        break
if 21 == total:
    print("you won!")
if 21 < total:
    print("you bust.")
if hit == 'n':
    print("your total is ", total)


What i want to happen is
1. take input "y" or "n"
2a. if input "y" then give add another number. repeat until input "n" or the number(the second total variable(i changed it so that the while loop should recognize it)) exceeds 21 or player reaches 21 exactly.
2b. if input "n" print the total.


Here are the problems
1. nothing happens upon inputing "n". when i ctrl c i get this
Code: Select all
^CTraceback (most recent call last):
  File "21.py", line 8, in <module>
    while total < 21:
KeyboardInterrupt

2a. If i keep the "break" then it only prints once. i want it to give the option of adding another number. i would think it would do this because it is in a while loop.
2b. if i get rid of the break it just keeps printing.
3. in number 2a sometimes when repeating the number will exceed 21 yet it won't exit the loop. why is this?



Thank you for your help!
We all know that light travels faster than sound. That's why certain people appear bright until you hear them speak. -Albert Einstein

Formerly known as Luckydog.

Looking for me? Check the IRC I've been hanging out there.
User avatar
RETNUH
Level 4
Level 4
 
Posts: 450
Joined: Fri Jul 15, 2011 10:55 pm
Location: Nebraska, USA

Linux Mint is funded by ads and donations.
 

Re: A few simple(I think) python3 problems

Postby Pilosopong Tasyo on Tue Dec 13, 2011 7:44 am

...nothing happens upon inputing "n".

Something is indeed happening. It's called an infinite loop. There is no provision within the loop that handles an "n" hit.

...the number will exceed 21 yet it won't exit the loop. why is this?

Logic error. Pay attention to the following:

Code: Select all
while total < 21
...
total2 = total + c

How is the total variable updated?
---
I think you will have to rewrite the whole thing. Rethink how it should be implemented. I see a lot of logic errors in the program that doesn't make sense. Variables that don't go anywhere/only used once. Illogical sequence of events.

I don't use Python, but I'm under the impression that Python uses indentation in lieu of explicit reserved words (e.g. begin...end in Pascal, if...fi in Bash, etc.) or symbols (e.g. {...} in C) in defining a code block?

In any programming task I write the algorithm first and see if it does what it's intended to do, then code it. I advise you do the same.

P.S. I was tempted to write the solution to this programming task earlier, but that only meant I'm depriving you of the adventure in figuring out the logic of this simple program. :mrgreen: :P
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 5
Level 5
 
Posts: 800
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: A few simple(I think) python3 problems

Postby Garvan on Tue Dec 13, 2011 8:36 am

Use

hit = raw_input("Hit? y or n")

And print out every value, so you can see what is happing.

I think Pilosopong Tasyo is being a bit harsh on a learner when he said "I think you will have to rewrite the whole thing.". It was a good try, and only a few small edits are needed to get it to work.

Garvan
Desktop: Dell, Intel Core 2 Duo E7500 @ 2.93Ghz, 2GiB RAM, Linux Mint 13 Maya.
Net-book: SAMSUNG N148-Plus, 2x Intel Atom CPU N450 @1.66Ghz, 1GB RAM, lubuntu 12.04.
User avatar
Garvan
Level 4
Level 4
 
Posts: 274
Joined: Sun May 29, 2011 3:26 am
Location: Cambodia

Re: A few simple(I think) python3 problems

Postby Pilosopong Tasyo on Tue Dec 13, 2011 9:19 am

Garvan wrote:I think Pilosopong Tasyo is being a bit harsh on a learner.

The reason why I don't want to give the solution outright was because no one knows for sure if this is supposed to be a school programming assignment and the OP was just looking for quick answer without thinking what's wrong with the code. I have to take that into consideration. If you have noticed, the use of certain smileys at the end of my reply earlier sets the tone of my post. Everyone have their own style of teaching, and this is how I do mine. I don't want to just spoonfeed the OP. But if I see that the whole code is potentially flawed at the get go then I wouldn't hesitate pointing that out, since it would be futile to fix something if the logic is already wrong in the first place and it only wastes the OP's time.

You may consider my method a tad harsh, I wonder what the OP thinks of it or someone else's opinion. If the consensus is what you said, then mea culpa.
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 5
Level 5
 
Posts: 800
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: A few simple(I think) python3 problems

Postby RETNUH on Tue Dec 13, 2011 3:03 pm

Pilosopong Tasyo wrote:
Garvan wrote:I think Pilosopong Tasyo is being a bit harsh on a learner.

The reason why I don't want to give the solution outright was because no one knows for sure if this is supposed to be a school programming assignment and the OP was just looking for quick answer without thinking what's wrong with the code.


Reasonable. It isn't for school, I'm just trying to learn to program.

But I have thought about it and can't figure it out :(

[qoute="Garvan"]Use

hit = raw_input("Hit? y or n")[/qoute]
This is python 3 i'm talking about. There isn't a raw_input function any more. But I get the point :)




I'll do a rewrite and see what happens. If i still have problems I'll post in this thread.
We all know that light travels faster than sound. That's why certain people appear bright until you hear them speak. -Albert Einstein

Formerly known as Luckydog.

Looking for me? Check the IRC I've been hanging out there.
User avatar
RETNUH
Level 4
Level 4
 
Posts: 450
Joined: Fri Jul 15, 2011 10:55 pm
Location: Nebraska, USA

Re: A few simple(I think) python3 problems

Postby Pilosopong Tasyo on Tue Dec 13, 2011 9:24 pm

Luckydog wrote:Reasonable. It isn't for school, I'm just trying to learn to program. But I have thought about it and can't figure it out :( I'll do a rewrite and see what happens. If i still have problems I'll post in this thread.

Alrighty, at least there's the effort. Moving on.

If you can resist the temptation in taking a peek at the algorithm I wrote, then don't click this link. :lol: It's similar to the flow of your code but with the proper logic and including what's only necessary. So, only click the link above as a last resort. :P
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 5
Level 5
 
Posts: 800
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: A few simple(I think) python3 problems

Postby RETNUH on Wed Dec 14, 2011 1:59 am

Pilosopong Tasyo wrote:Alrighty, at least there's the effort. Moving on.

If you can resist the temptation in taking a peek at the algorithm I wrote, then don't click this link. :lol: It's similar to the flow of your code but with the proper logic and including what's only necessary. So, only click the link above as a last resort. :P

I couldn't resist. Problem is is for all the giving in I did it didn't help. It appears I don't have access to the page you gave me. I made an account and that didn't help. Is it on a private account type thing?
We all know that light travels faster than sound. That's why certain people appear bright until you hear them speak. -Albert Einstein

Formerly known as Luckydog.

Looking for me? Check the IRC I've been hanging out there.
User avatar
RETNUH
Level 4
Level 4
 
Posts: 450
Joined: Fri Jul 15, 2011 10:55 pm
Location: Nebraska, USA

Re: A few simple(I think) python3 problems

Postby Pilosopong Tasyo on Wed Dec 14, 2011 2:19 am

Luckydog wrote:It appears I don't have access to the page you gave me. I made an account and that didn't help. Is it on a private account type thing?

Hmm... that's odd. I did noticed earlier that Dropbox didn't give me the option to copy the public link. So I had to go to the webpage and copy the link from there instead. I thought Dropbox was updating something on their end so didn't think much of it. But anyway, I uploaded it to pastebin instead. You can view the algorithm here. The Bash version is here.

If you have questions, don't hesitate to ask. HTH.
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 5
Level 5
 
Posts: 800
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: A few simple(I think) python3 problems

Postby Garvan on Wed Dec 14, 2011 7:46 am

I don't normally program in python, so I missed the new python 3 input function. I should have realized from the context. Pilosopong Tasyo is correct about being wary of doing peoples homework, but I looked at your number of posts on the forum, and decided you had not come here for that purpose.

I have not played 21 since I was a kid, but I remember J, Q, K all had a value of 10, and A could be either 1 or 11 at the players choice. And if you are dealt 5 cards under 21 then that was called a five-card trick.

Does a five-card trick beat 21? I have forgotten, and I and not sure if 21, pontoon and black jack have the same rules.

Anyway your random numbers are in the range 0 to 12 (inclusive!). This looks wrong, so you still have a bit of fun to get it working. And you could add code to make sure somebody is not dealt five aces in a row.

Garvan
Desktop: Dell, Intel Core 2 Duo E7500 @ 2.93Ghz, 2GiB RAM, Linux Mint 13 Maya.
Net-book: SAMSUNG N148-Plus, 2x Intel Atom CPU N450 @1.66Ghz, 1GB RAM, lubuntu 12.04.
User avatar
Garvan
Level 4
Level 4
 
Posts: 274
Joined: Sun May 29, 2011 3:26 am
Location: Cambodia

Re: A few simple(I think) python3 problems

Postby RETNUH on Wed Dec 14, 2011 8:18 pm

Pilosopong Tasyo wrote:If you have questions, don't hesitate to ask. HTH.

Ok. Thank you! I think what i'll do is put that in python 3, see what that looks like, and probably tinker with it a bit. Thanks again!

Garvan wrote:I don't normally program in python, so I missed the new python 3 input function.


If you ever want to use python 3 look at this to update yourself from python 2 to python 3

Garvan wrote:I have not played 21 since I was a kid, but I remember J, Q, K all had a value of 10, and A could be either 1 or 11 at the players choice. And if you are dealt 5 cards under 21 then that was called a five-card trick.

Does a five-card trick beat 21? I have forgotten, and I and not sure if 21, pontoon and black jack have the same rules.

I'm going to add more once i have the basic part done.

Garvan wrote:Anyway your random numbers are in the range 0 to 12 (inclusive!). This looks wrong, so you still have a bit of fun to get it working. And you could add code to make sure somebody is not dealt five aces in a row.
Similarily to range() the first number is included but the second isn't. see [url=http://docs.python.org/release/3.1.3/library/random.html[/url]
We all know that light travels faster than sound. That's why certain people appear bright until you hear them speak. -Albert Einstein

Formerly known as Luckydog.

Looking for me? Check the IRC I've been hanging out there.
User avatar
RETNUH
Level 4
Level 4
 
Posts: 450
Joined: Fri Jul 15, 2011 10:55 pm
Location: Nebraska, USA

Re: A few simple(I think) python3 problems

Postby Garvan on Wed Dec 14, 2011 10:35 pm

Luckydog wrote:<snip>
Garvan wrote:Anyway your random numbers are in the range 0 to 12 (inclusive!). This looks wrong, so you still have a bit of fun to get it working. And you could add code to make sure somebody is not dealt five aces in a row.
Similarily to range() the first number is included but the second isn't. see http://docs.python.org/release/3.1.3/library/random.html


This is what your link tells me:
random.randint(a, b)
Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).


Garvan
Desktop: Dell, Intel Core 2 Duo E7500 @ 2.93Ghz, 2GiB RAM, Linux Mint 13 Maya.
Net-book: SAMSUNG N148-Plus, 2x Intel Atom CPU N450 @1.66Ghz, 1GB RAM, lubuntu 12.04.
User avatar
Garvan
Level 4
Level 4
 
Posts: 274
Joined: Sun May 29, 2011 3:26 am
Location: Cambodia

Re: A few simple(I think) python3 problems

Postby RETNUH on Mon Dec 19, 2011 5:44 pm

For anyone interested I finally got it working a couple days ago. I also added a dealer. You can find what I wrote here

Thanks again to Pilosopong Tasyo and Garvan who helped me out.
We all know that light travels faster than sound. That's why certain people appear bright until you hear them speak. -Albert Einstein

Formerly known as Luckydog.

Looking for me? Check the IRC I've been hanging out there.
User avatar
RETNUH
Level 4
Level 4
 
Posts: 450
Joined: Fri Jul 15, 2011 10:55 pm
Location: Nebraska, USA

Linux Mint is funded by ads and donations.
 

Return to Open chat

Who is online

Users browsing this forum: No registered users and 7 guests