This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2008-03-06 07:23:56

rchakma
Scratcher
Registered: 2008-03-03
Posts: 1

How could I code this problem in scratch

Here is the problem that I want to code in scratch:-
"Prompt a user to enter a number. Get the number, add 5 to it and then double the sum. Display the final number after doubling."

I couldn't figure out two things: - (1) How to get a number (say 3) from a user and then assign it to a variable (say number1), and then (2) How to add 3 to number (number1+3)

Would someone help me in coding this in scartch, please?

Rana

Offline

 

#2 2008-03-06 08:30:24

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: How could I code this problem in scratch

The easiest way to get a number from the user is to create a variable and display it on the screen.  Then, right-click on the variable and set it to Slider mode which will allow the user to enter a value from zero to 100.  Right-click again and you can set the limits of the slider, if you want something different.  If you are using a Mac computer, I think it is Opt-Click or something instead of Right-click.

So with the slider variable, you will probably want go have a starting value set on the Green Flag

[blocks]
<when green flag clicked>
<set{ Number1 }to( 0
[/blocks]

You can use the default cat sprite to do the calculation and display the output when it is clicked

[blocks]
<when[ Sprite 1 ]clicked>
<set{ Number 1 }to( (( 2 <*> (( Number 1 <+> 5 )) ))
<say[ Number 1 ]for( 10 )secs>
[/blocks]

Last edited by Paddle2See (2008-03-06 08:31:14)


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#3 2008-03-06 12:42:27

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: How could I code this problem in scratch

You can use a bank of "If (number key) pressed" statements in a loop to input a multi digit number, or put a numeric keypad made of sprites on screen.


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#4 2008-03-09 09:19:59

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: How could I code this problem in scratch

You can find numeric keypad entry already written for you in various calculator projects, for example, my RPN calculator:

http://scratch.mit.edu/projects/kevin_karplus/76772

Offline

 

Board footer