11 Replies - 786 Views - Last Post: 16 June 2015 - 12:22 PM Rate Topic: -----

#1 sigint-ninja  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 05-June 15

starting to realize how important pseudo code is...

Posted 15 June 2015 - 01:45 PM

Hi guys,

real newb at python...reading a good book

im at the all familiar coin toss exercise

im battling a bit with the logic...so i thought i would focus more on the pseudo code...just to try understand
what needs to happen...i know its a simple program and you wouldnt typically right out pseudo code for such a mickey
mouse thing...my friend who is a third year in a computer science degree says its a waste of time and that he never does that...do you guys really plan your work or is it something a beginner does more so...or does it really help the more complicated a program
becomes...looking forward to your input...

ok, what i have so far...

the computer asks you how many coins you want to flip
it then converts this to an integer and stores it in a variable called coins

then i can have two random numbers ods can be heads, evens can be tails.
so if its evens heads variable gets a plus 1 ...same for ods
random can generate numbers from 1 to 10
then i need to have this action repeated as many times as the coins selected
finally i can get a print out of all the data at the end
coins flipped =
heads =
tails =
etc

am i going about this all wrong? is there an easier way?

Is This A Good Question/Topic? 0
  • +

Replies To: starting to realize how important pseudo code is...

#2 witeboy724  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 208
  • Joined: 21-June 12

Re: starting to realize how important pseudo code is...

Posted 15 June 2015 - 02:02 PM

I don't really understand the question...

Writing out pseudo code can help you understand the steps you need to take to get from one line to the next in the code. I imagine it's helpful for beginners or anyone working on a big project. I personally don't do much of that. Rather, I find it very helpful to write a bunch of comments in the code to remind myself what lines are doing, especially on the more complex looking lines.

I think your plan sounds good so far. I would recommend looking into random.choice for flipping and a quick tutorial on loops for multiple flips. Happy coding, python is awesome
Was This Post Helpful? 1
  • +
  • -

#3 sigint-ninja  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 05-June 15

Re: starting to realize how important pseudo code is...

Posted 16 June 2015 - 02:31 AM

hi guys,

coin toss program...
here is my code
###coin flip program###
import time
import random
time.sleep (2)
coins = int(input("How many coins do you want to flip?"))
time.sleep (2)
print("Flipping the coins...")
#the coin flip
heads = 0
tails = 0
flip = random.randint(1,2)
if flip==1:
    heads=heads+1
if flip==2:
    tails=tails+1


how would i add a loop to get flip to repeat the value of coins...

please dont give me code...just the logic side and a few keywords
thanks

This post has been edited by baavgai: 16 June 2015 - 03:28 AM
Reason for edit:: tagged

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6995
  • View blog
  • Posts: 14,629
  • Joined: 16-October 07

Re: starting to realize how important pseudo code is...

Posted 16 June 2015 - 03:35 AM

Make sure you use code tags here, particularly for Python, which is particularly white space particular.

Where to add the loop... See where you init heads and tails to 0? Well, that would logically be the last thing you do before you enter a loop. The flip and ifs would go inside.

Also, if your random flip is only two possible values, you needn't use two ifs. e.g.
if random.randint(1,2)==1:
    heads += 1
else:
    tails += 1



Why 1,2, btw? Programmers start counting at zero. Indeed, if you randomly pick between zero and one, you could more easily use a list...

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#5 sigint-ninja  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 05-June 15

Re: starting to realize how important pseudo code is...

Posted 16 June 2015 - 05:08 AM

"Make sure you use code tags here, particularly for Python, which is particularly white space particular."
can you please explain a little more...tried googling it...

and thanks for the help with if sattements...yes i see its much better
also i will start with 0 in future...not 1 etc

thanks for your help
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12176
  • View blog
  • Posts: 45,242
  • Joined: 27-December 08

Re: starting to realize how important pseudo code is...

Posted 16 June 2015 - 07:39 AM

Quote

"Make sure you use code tags here, particularly for Python, which is particularly white space particular."
can you please explain a little more...tried googling it...

You can use code tags like so: :code:. Their usage is shown when you try to post.

Quote

how would i add a loop to get flip to repeat the value of coins...

Have you looked at loops yet? I'd start there. Then make an attempt to implement it yourself. If you're still stuck after that, feel free to post with your attempt, thoughts, and a clear description of your specific problems/errors.

Best of luck! :)
Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12176
  • View blog
  • Posts: 45,242
  • Joined: 27-December 08

Re: starting to realize how important pseudo code is...

Posted 16 June 2015 - 07:42 AM

Quote

my friend who is a third year in a computer science degree says its a waste of time and that he never does that...do you guys really plan your work or is it something a beginner does more so...or does it really help the more complicated a program

Pseudo-code is useful in describing algorithms in a language-agnostic manner. For large programs, we don't hash out pseudo-code. However, design and planning are important. There are concerns about how components interact, what each component does, separation of responsibilities, etc. At this stage, you aren't likely thinking about those things and (to a large extent) don't need to do so. Once you hit functions, you'll begin thinking more about separation of concerns.

Also, I'll merge this with your existing thread.
Was This Post Helpful? 1
  • +
  • -

#8 sigint-ninja  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 05-June 15

Re: starting to realize how important pseudo code is...

Posted 16 June 2015 - 11:46 AM

thanks for all the help so far...great community here!!!

i saw some operators like += but it didnt explain what it was doing...

am i better implementing a for loop or while loop in this senario...

thanks again
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12176
  • View blog
  • Posts: 45,242
  • Joined: 27-December 08

Re: starting to realize how important pseudo code is...

Posted 16 June 2015 - 11:48 AM

See this tutorial on operators in Python. The += operator works as follows:
a += b //is the same as 
a = a + b



While and for loops can be used interchangeably. Generally, a for loop is used when you want to iterate a fixed number of times. A while loop is better when you want to terminate after a certain condition, which may not depend on the number of iterations.

While and for loops can be used interchangeably, though.
Was This Post Helpful? 1
  • +
  • -

#10 sigint-ninja  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 05-June 15

Re: starting to realize how important pseudo code is...

Posted 16 June 2015 - 12:16 PM

i spent the last 40 minutes trying to get the loop to work...my book only really shows loops with strings not numbers...at this stage

the best i could come up with:

###coin flip program###
import time
import random
time.sleep (2)
coins = int(input("How many coins do you want to flip?"))
time.sleep (2)
print("Flipping the coins...")
#the coin flip
heads = 0
tails = 0
while coins !=0:
    flip = random.randint(1,2)
if random.randint(1,2)==1:
    heads += 1
else:
    tails += 1
coins=coins-1
print (heads)
print (tails)


my reasoning is such:
coins is the total the user inputs
while the coins are not 0...flip them, after each flip minus 1 from the coins total...till it reaches 0
then print the heads and tails from those flips...

stumped...only prints out 1 heads no tails, or vice versa...

does each line of code need to be put in [] ...sorry

This post has been edited by macosxnerd101: 16 June 2015 - 12:18 PM
Reason for edit:: Fixed code tags: [code] Your Code Goes Here [/code]

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,888
  • Joined: 12-December 12

Re: starting to realize how important pseudo code is...

Posted 16 June 2015 - 12:20 PM

How to use code tags:

Attached Image
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12176
  • View blog
  • Posts: 45,242
  • Joined: 27-December 08

Re: starting to realize how important pseudo code is...

Posted 16 June 2015 - 12:22 PM

Remember that in Python, indentation matters. So the while loop only encapsulates the statement flip = random.randint(1,2).

Next, your logic doesn't make much sense. Logically, coins should be at least 0 when the user inputs a value, right? However, you increment coins = coins + 1. I assume you are trying to do this on each iteration of the loop. Even once you fix your indentations, this is not correct. It would create an infinite loop. Do you see why that is?
coins = int(input("How many coins do you want to flip?"))
...
while coins !=0:
    flip = random.randint(1,2)
if random.randint(1,2)==1:
    heads += 1
else:
    tails += 1
coins=coins-1


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1