This is the P2PU Archive. If you want the current site, go to www.p2pu.org!
You haven't posted any discussions yet.
Please review Expressions, Statements, and Names and the learning resources section. Be sure to complete the assigned reading and any applicable excercises as we are here to practice. If you have any questions or difficulties post them here and we will work together to make sure that everyone has a positive learning experience.
Please also post ideas and insights into these concepts. We can learn a lot from each others' perspectives and experience :-)
type for hour and rate is string so i change their type
see below.
this is what i've got:
>>>hour = raw_input("Enter hours worked:")
>>>rate = raw_input("Enter rate:")
>>>pay = int(hour) * float(rate)
>>>print pay
Ah, nice work. What happens when you multiply a string by an integer? e.g.
>>> "This is a nice string." * 3
You get an error.
Try entering the following in your interactive Python prompt (omitting the '>>>'):
>>> "You get an error." * 3
Huh I thought it couldn't mix but it multiplies the string. It creates a new string containing the original string in the amount put in.
>>> "you get an error" * 3
'you get an erroryou get an erroryou get an error'
>>>c = raw_input('Convert Celsius to Fahrenheit\n Enter Temperature: ')
>>>conversion = int(c) * 9.0/5.0 + 32
>>>print conversion
Sally,
Nice work :)
What other conversions might be handy to perform with Python?
--Brylie
Brylie, thanks for the comment and direction. Anything that could be quantified would be useful to convert (I often find I have to look up imperial measurements). I will think about it some more and see how I can put it to use for my own purposes.
I cannot seem to get my IDLE to print out the answer for all the exercises for chapter 2. I installed Python 2.7
what do you mean by print out. Don't you select the answers you put into idle, got up to the edit menu, copy, and paste into the posting box on this forum?
Can you give us a specific example please Wesley?
I'm having trouble with the raw input. I keep getting syntax errors.
>>> Name = raw_input()
Traceback (most recent call last):
File "", line 1, in
Name = raw_input()
NameError: name 'raw_input' is not defined
You may need to use 'input()' instead of 'raw_input()', if you are using Python 3.
I hope to learn more about programming! I have a feeling this course will be great, better than some of the college courses I've had. Has anyone had an issue with installing Python on Windows 7?
I'd like to suggest that we use http://pastie.org for code samples. It's free easy to use etc. Here's my attempt at chapter two exercises: http://pastie.org/1835472
I threw my end of chapter exercises up on my github page - if you run the script all should work - plus try/except blocks for error handling - http://www.tylercipriani.com/python101/
Thank You ....
I don't know where to put my homework so I'll stick it here.
Exercise 2.2
print('Enter your name')
Name = input()
print('Welcome ' + Name)
Exercise 2.3
print('Please enter how many hours you have worked in numbers')
HoursWorked = input()
print('Please enter how much you earn per hour in dollars without the dollar sign')
HourlyWage = input()
Payment = int(HoursWorked) * int(HourlyWage)
print('The amount of money you have earned is $' + str(Payment))
Exercise 2.4
1. 8.5 Floating Point
2. 8.5 Floating Point
3. 4.0 Floating Point
4. 11 Integer
Exercise 2.5
print('What is the temperature in celsius?')
TempCelsius = input()
TempFharen = (float(TempCelsius)*(9/5))+32
print('It is ' + str(TempFahren) + ' in Fahrenheit')
while doing this, the word hour felt weird. It feels like it's spelled funny.
Until further notice I will follow the example of Jim Roma. You can find my homework at http://pastie.org/1843656
was doing the 2.3 excercise but the round thing is not working:
>>> print "Pay: %f" % round((float(raw_input("Enter Hours: "))*float(raw_input("Enter Rate: "))),2)
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.250000
hmm it seems it was the %f going from 96.25 to 96.25000 i changed it to %s and worked..
>>> print "Pay: %s" % round( float(raw_input("Enter Hours: ")) * float(raw_input("Enter Rate: ")) ,2 )
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25
Well, that could be because 35*2.75 is exactly 96.25 - check out: http://diveintopython.org/native_data_types/formatting_strings.html and look at formatting numbers. I ended up using %.2f" % round(rate, 2) - so a combination of methods - that's the only thing I could get to work.
I need more practice with github, so you'll find my week 1 answers here:
https://github.com/brotherhutch/python101/tree/master/chapter2
#Exercise 2.2
>>> name = raw_input("What is your name? \n")
What is your name?
Chuck
>>> print "Hello, " + name
Hello, Chuck
#Exercise 2.3
>>> hours = raw_input("Enter your hours: ")
Enter your hours: 35
>>> rate = raw_input("Enter your rate: ")
Enter your rate: 2.75
>>> pay = float(hours) * float(rate)
>>> print pay
96.25
#Exercise 2.4
1) 8
2) 8.5
3) 4.0
4) 11
#Exercise 2.5
>>> celsius = raw_input("Enter a Celsius Temperature: ")
Enter a Celsius Temperature: 100
>>> #100 degrees C is the boiling point of water
>>> final_temp = float(celsius)
>>> fahrenheit = (9.0/5) * final_temp + 32
>>> print fahrenheit
212.0
>>> #212 F is definitely the boiling point of water...
I'm starting to see the simplicity of python. In C++, you have to #include classes and using namespace std; just to print things.
Question: is there any way to like repeat what i typed previously? When i get errors, I have to type out everything again. I'm using the Python IDLE.
Jeremy, you could just save your small program as a .py script. From IDLE, go to file and new.
http://pastie.org/1852133 My exercises can be found there.
Answer to Ex. 2.5
# convert Celsius temperature to Fahrenheit and display new temperature
# equation is Tf = (9/5) * Tc + 32
celsius = raw_input('Enter temperature is degrees Celsius: ')
fahrenheit = (9.0/5) * float(celsius) + 32
print 'Temperature in degrees Fahrenheit is: ', fahrenheit
Python3 Homework the exercises in the book that I wrote:
#!/usr/bin/python
#Filename: str_format.py
age = 25
name = 'Swaroop'
print('{0} is {1} years old'.format(name, age))
print('Why is {0} playing with that python?'.format(name))
D:\Programming Projects\Python>str_format.py
Swaroop is 25 years old
Why is Swaroop playing with that python?
# Filename : var.py
i = 5
print(i)
i = i + 1
print(i)
s = '''This is the multi-line string.
This is the secondline.'''
print(s)
D:\Programming Projects\Python>var.py
5
6
This is the multi-line string.
This is the secondline.
# Filename: expression.py
length = 5
breadth = 2
area = length * breadth
print('Area is', area)
print('Perimeter is', 2 * (length + breadth))
D:\Programming Projects\Python>expression.py
Area is 10
Perimeter is 14
I am not able to type a variable in a .py file, but am able to do so in the shell. using python 3.2. any ideas? thanks
Can you post an example code snippit where a variable assignment doesn't work? E.g.
#!/usr/bin/python3
# Broken_variable_example.py
# Save a variable
a_variable = "A value."
# Print the variable
print(a_variable)
here is my work for chapter 2
Ex 2.2
name = input('enter your name\n')
print ('Hello ' + name)
""" output below
>>>
enter your name
mark
Hello mark
>>>
"""
Ex 2.3
hours = input ('Enter Hours: ')
rate = input ('Enter Rate: ')
#pay = int(hours)
pay = int(hours) * float(rate)
print ('Pay: ' + str(pay))
""" output below
>>>
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25
>>>
"""
Ex 2.4
"""
width = 17
height = 12.0
1. width/2 8.5, this is a float
2. width/2.0 8.5 this is a float
3. height/3 4, this is an float
4. 1 + 2 * 5 11, this is an int
"""
width = 17
height = 12.0
#1.
print(width/2)
#2.
print(width/2.0)
#3.
print(height/3)
#4.
print(1+2*5)
""" output below
>>>
8.5
8.5
4.0
11
>>> type (width/2)
>>> type(width/2.0)
>>> type(height/3)
>>> type (1+2*5)
>>>
"""
Ex 2.5
#program to convert degress C into degrees F, using F = 9/5C + 32
C = input('input temparture in degrees celsius ')
F = 9/5 * int(C) + 32
print(F)
""" output below
>>>
input temparture in degrees celsius 100
212.0
>>> ================================ RESTART ================================
>>>
input temparture in degrees celsius -40
-40.0
>>> ================================ RESTART ================================
>>>
input temparture in degrees celsius 0
32.0
>>>
"""
# Excercise: 2.3
hours = raw_input('Enter Hours: ')
rate = raw_input('Enter Rate: ')
pay = float(hours) * float(rate)
print 'Pay: ' , pay
# re-factored
pay = float(raw_input('Enter Hours: ')) * float(raw_input('Enter Rate: '))
print 'Pay: ' , pay
ex. 2.2
name = raw_input('Enter your name: ')
print 'Hello, +name
***************
ex. 2.3
hours = raw_input('Enter Hours Worked: ')
rate = raw_input('Enter Rate: ')
pay = int(hours) * float(rate)
print 'Pay: '+ str(pay)
***************
ex. 2.4
1. 8
2. 8.5
3. 4.0
4. 11
*****************
ex. 2.5
Tc = raw_input (Enter Temperature in Celsius: ')
Tf = (9.0/5.0) * float(Tc) + 32
print Tc + (' degrees Celsius = ') + str(Tf) + (' degrees Fahrenheit')
# Excercise 2.5
print (float(raw_input( 'Enter Celsius value to convert to Fahrenheit. ')) * 1.8) + 32
# Excercise 2.4
width = 17
height = 12.0
print "For width = ",width,", width/2 is ",width/2 , " and type is", type(width/2)
print "For width = ",width,", width/2.0 is ",width/2.0 , " and type is", type(width/2.0)
print "For height = ",height,", height/3 is ",height/3 , " and type is", type(height/3)
print "1 + 2 * 5 is ",1 + 2 * 5," which is a value of type ",type(1 + 2 * 5)
I have a general question about the creation of variables.
Is it good practice to minimise the number of variables created in a program. It seems to me that creating a redundant variable increases the chances of an error somewhere and makes the code more convoluted than it needs to be. e.g. I don't see the point of creating a variable for something that won't change in value and is only needed once. But I see this done a lot by some very experience coders; although I rarely see it in books.
Variables are created for a multitude of reasons - the reason on which you've touched is to DRY (Don't Repeat Yourself) out your code (i.e. you create a variable rather than write a value multiple times); however, there are other reasons to create variables. The main reason I might create a variable for a number I'm using once is that I don't like having 'magic numbers' inside my code blocks (see definition #3 here: http://en.wikipedia.org/wiki/Magic_number_(programming)). What I mean is that giving a variable a meaningful name like page_width or name_iterator can help you when you look back at code you've written several years ago, or help someone who's new job is to look at code you've written several years ago. Well commented code is another way around this, but, I feel, comments can sometimes become a crutch for poor variable naming.
This is just my opinion. I'm sure there are other schools of thought on this. Best practice is defined as the strong opinions agreed upon by many experts - a good Google search might be 'variable naming best practice'
I stumbled across this document when delving a bit deeper into the idea of best practice for Python - it's great reading if you're interested using Python in your line of work: http://www.python.org/dev/peps/pep-0008/
I think it depends on the scope of the variable. having a bunch of unneeded global variables seems sloppy, but if a variable only exists in a certain function/object and makes things easier and more clear, then I think it is definitely a good idea. I do not know much about variables in python though, so I might be wrong.
Oops, bit late with these
Exercise 2.2
name = raw_input("Enter your name: ")
print ''.join(['Welcome ', name])
Exercise 2.3
rate = raw_input("Enter rate:")
hours = raw_input("Enter hours:")
pay = float(hours)*float(rate)
print ''.join(['Pay = ', str(pay)])
Exercise 2.5
temp_celsius = float(raw_input("Enter the temperature in Celsius: "))
temp_fahrenheit = temp_celsius*1.8 + 32
print ''.join(['Temperature in Fahrenheit: ', str(temp_fahrenheit)])
Not a problem. Good work!
which python version are you using