HomePortalFAQSearchMemberlistUsergroupsRegisterLog in

Share | 
 

 If/Else and User Input

Go down 
AuthorMessage
Compu-Teck Inc.



Posts : 80
Join date : 2010-04-29
Age : 26

PostSubject: If/Else and User Input   Thu Sep 16, 2010 1:43 pm

Hello, all! CTI here with another Python tutorial. In this tutuorial, we will cover the if, else, and elif statements, as well as asking users for input. Here is what the code would look like (Red text is the user input, Green text is what will show in the input window for Python) :

Code:

number=23
guess=input ("Guess the number!")

if guess == number:
  print ("Nice job! You guessed it!") #A new block starts here
  print ("Unfortunately, you don't get anything") # The new block ends here
elif guess < number:
  print ("No, it is a little higher than that...")
else:
  print ("No, it is a little lower than that...")
print ("Done!")

Output:
Guess the number!
20
No, it is a little higher than that...
Done!
>>>

Guess the number!
25
No, it is a little lower than that...
Done!
>>>

Guess the number!
23
You guessed it!
Unfortunately, you don't get anything
Done!
>>>


Here is how the code works!
Code:

number=23
guess=input ("Guess the number!")
In these two lines of code, we have three things happening. First, the variable "number" is assigned the value of 23. Next, the variable "guess" is assigned a value determined by the user. This is where the input command comes in. When this shows up, you are telling Python to bring up an interactive input window where the user can type in something using the keyboard, in this case, a number.
Code:

if guess == number:
  print ("Nice job! You guessed it!") #A new block starts here
  print ("Unfortunately, you don't get anything") # The new block ends here
Next, we are telling Python if the variable "guess" (given by the user) is equal to (== [as opposed to not equal to {!=}]) the variable "number", then the two strings are to be printed. Also, we have a comment in place. Users do not see comments, but comments are used by programmers to make notes as to why something is there, or to explain how the code works.
Code:

elif guess < number:
  print ("No, it is a little higher than that...")
Ah, "elif". This is one of the reasons I like Python. Elif is basically another if/else inside of the else of the first if/else. It makes the code look better, and is much easier to type. In this case, we are telling Python that if the guess is less than the number, it is to print the string given.
Code:

else:
  print ("No, it is a little lower than that...")
Here, we are ending the if/else statement by telling Python that if none of the above guidelines are met, then it is to display the alternate string. If you do not want anything to happen, you would just print "Pass" in the block beneath the else. That would tell Python that it is to ignore the empty block below else.
Code:

print ("Done!")
This goes back to my first tutorial. We are telling Python to display the string "Done!" to signify the end of the program.

NOTE: ANY CODE DISPALYED FROM THIS TUTORIAL ON OUT WAS MADE IN PORTABLE PYTHON V1.1(EQUIVALENT TO PYTHON V3.0.1! CODE FOR AN ACTUAL PYTHON PROGRAM WILL DIFFER!
Back to top Go down
View user profile
gamerboy

avatar

Posts : 37
Join date : 2010-08-27
Age : 34
Location : Germany

PostSubject: Re: If/Else and User Input   Tue Sep 21, 2010 7:24 pm

your very important bold text needs to be spell checked. (if you have winblows) i have linux, and i found a way to use the REAL visual studio 2010 on it Very Happy had to make my own V.M., WINE didn't cut it.
Back to top Go down
View user profile http://gamerboy8864hacks.freeforums.org
Excimer Sun Software

avatar

Posts : 152
Join date : 2010-05-07
Age : 26
Location : USA

PostSubject: Re: If/Else and User Input   Sat Sep 25, 2010 5:33 pm

I <3 Linux. Especially Ubuntu 10.04 Smile. What flavor do you use?
Back to top Go down
View user profile
Sponsored content




PostSubject: Re: If/Else and User Input   

Back to top Go down
 
If/Else and User Input
Back to top 
Page 1 of 1
 Similar topics
-
» Game names vs user names - MMUFF
» Ebay preorder problems, user MMATT
» DCUC Matty collector exclusives
» moving to new ipad
» Aquarium Book Adapter

Permissions in this forum:You cannot reply to topics in this forum
Compile Crazy :: Programming :: Python :: For Beginners-
Jump to: