![]() |
Novice questions.
Hello all,
I've enjoyed looking over the forums the past couple days. You have a great community. What brought me here was the need for custom scripts to make my life easier (necessity is the mother of invention, right?). I'm in charge of enterprise backups using Legato. We have some scripts (that look like they were written in C?) that were made years ago but they need tweaking. I would also eventually like to write some scripts to aid me in daily tasks that I am accomplishing now through the GUI, which is extremely time-consuming. I also know and use the equivalent unix commands to what I do through the GUI which is my I'm eager to learn Python. I've avoided programming my entire life after trying to learn C at age 12 (traumatizing). I'm much more logical now and figure it's a good skill to have plus I could really use it. So, please bear with my n00b questions. :) Anyway, this is my third day into Python. I'm using 'O'Reilly: Learning Python, 2nd Edition' and 'Non-Programmers Tutorial For Python' as well as DrPython (much more enjoyable than IDLE). So, one of the exercises I was assigned was to "Write a program that gets 2 string variables and 2 integer variables from the user, concatenates (joins them together with no spaces) and displays the strings, then multiplies the two numbers on a new line." I could just bang out the bare minimum but I understand better when it's something useful. This isn't too over-the-top. :
#!My Questions 1. In order for the math to come out right I need the tax_rate entered by the user to be a decimal (they enter 25 and is read by the program as .25). How can I accomplish this? 2. How do you insert a variable mid sentence and then have the sentence continue on? I can't quite figure that out. The last two lines I would like to combine into one but am getting syntax errors. 3. How can I clean up this code? Am I making something too difficult or can something be done more efficiently? I understand about hash bang and thought I would just include it in all my scripts for good practice (minus the path). Thanks a lot for your help. :D |
Quote:
Example: :
x = 25That would turn 25 in to 0.25 . Because 25 is converted to 25.0, then divided by 100.0 to get 0.25. Quote:
:
a = 1You could also concact them as strings: :
a = 1str() converts any type to string. Which is the only capable type of being concated. And I don't want to overload you, but the final and best (IMO) option is: :
a = 'One'%s represents a call to a string (but works with anything), %d a call to an integer Those calls are filled in after the literal inside %(). Quote:
The way you have it it's a bit scrambled. And your output is a little messy, but see the answer to #2 for answers to that. :
#!Congrats on your first program. Hope I helped. :) BTW. For future reference. input() can be a bad command to use in your programs. If they enter a character, your program crashes. Here's a quick way to fix that: :
try:except will except the mentioned error type and execute the indented block under except, as long as that error fell under the indented block in try. Understand? |
Sane,
Thanks a lot. A few follow-up questions. 1. Changing the type to floating did the trick but the number is always followed by six zeros (i.e. "Also, assuming a federal income tax rate of 25.000000, ...). While technically correct, how can I make this number less... exact. I would prefer 25.0. 2. You said that input() can be a bad command to use. What would you use in place of input()? Raw_input()? 3. Also, in my program can you specify where the "except NameError:" line should go? Do I need to place it after every line where they could enter a char instead of an int? I tried placing it at the very end but received a syntax error. Thanks for the help. |
Quote:
:
x = 3.141592Quote:
Quote:
:
def safe_input(x):I'm not sure if you've used functions yet. But that's a little example of how a function can improve the functionality of your code by breaking repeated steps. In this case the function is defined as "safe_input". And inside the () we call variable x, or the message that is passed through safe_input(). We return the value from the input call, but if the user entered a string. We use the function exit() from the import sys. The biggest advantage to using a function, is instead of using exit(), we can just go safe_input(x). That is something called a recursive function. The advantage of this, is it'll keep asking for the same input until it finally gets a number, or else it just recurses upon itself. :
def safe_input(x):It's a little complicated to go much more in to detail. Your tutorial will tell you all about functions, don't be intimidated. ;) |
| All times are GMT -5. The time now is 4:22 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC