Jump to content


#TM#

Member Since 10 Dec 2012
Offline Last Active Aug 27 2015 11:25 AM
-----

Posts I've Made

In Topic: Chpater 6, Transaction Pyware

19 August 2015 - 11:24 PM

you are welcome Bro...! smile.png


In Topic: Chpater 6, Transaction Pyware

18 August 2015 - 10:39 PM

Hi!

I've a qusetion about the program in chpater 6, page 184.

 In last line where else condition is lying:

    else:
        credit_card = input("Credit card number: ")
        save_transaction(prices[choice - 1], credit_card,items[choice-1])

What happens when save_transaction function is run? Please answer by focusing on given argument :

(prices[choice - 1], credit_card,items[choice-1])

 

Thanks friend!

Hi TheFuture, is that hard to understand ?  which part of it?

 

Anyway, if the customer don't choose quit which is in the if condition the else clause will run.

That means the customer wants to buy something.

 

In the else part , the save_transaction() will run; with the choice, price of choice and credit card number the customer entered.

 

Well, choice-1 is used here because python start indexing list/string/tuple etc at 0 (zero). Here is a link if you want to know why?! (Mr. Guido van Rossum written about it himself). But it will be difficult to understand if you don't know a little bit of computer science, algorithms, data structures etc. You may not be able to comprehend and may be like "What the hell is he talking about??!!" wacko.png

 

Guido van Rossum on why python start index at 0 (zero)

 

Anyway, let's get back to the program. You give choice to customer as 1,2,3... by using option=1 in the while loop, that is starting from 1. Obviously, in day to day life customers don't expect to encounter something like choice no. 0. tongue.png

 

So, now the function is called and the input customer provided are given as arguments to the save_transaction(price,credit_card,description).

where input to price argument is prices[choice-1]

input to credit_card argument is the credit_card number entered.

and input to description argument is the name of thing customer purchased,i.e., items[choice-1]

Now, the save_transaction() will write these above details to a file.

smile.png cool.png


In Topic: Chapter 4 Problem

14 July 2015 - 05:53 AM

I had a problem in chapter 4:
I wrote the code exactly as it is writen in the book




highest_score = 0
result_f = open("results.txt")
for line in result_f:
(name,score) = line.split()
if float(score) > highest_score:
highest_score = float(score)
result_f.close()
print("The highest score was:")
print(highest_score)



But the result I got from the shell was this:


>>>Traceback (most recent call last):
File "C:\Users\Victor\Desktop\Porcarias\Python\highest score.py", line 4, in <module>
(name,score) = line.split()
ValueError: need more than 0 values to unpack
>>>


Can anyone help me with this problem?

Hello Vitu, Can you please post your code using <> button on the editor sad.png  It is difficult to debug things. It may be an indentation problem. Who knows!?

 

Though in your case i think it is the results.txt file. I think you have probably opened and messed up with that file tongue.png . There will be a blank line on the top of that file. Open that file and delete that blank line. smile.png


In Topic: Chapter 4 Problem

13 July 2015 - 12:44 AM

 

this is the code I write it . It is exactly as what the book write.
 
 
scores=[]
result_f = open("results.txt")
for line in result_f:
    (name.score) = line.split()
    scores.append(float(score))
    
result_f.close()
 
print (scores[0])
print (scores[1])
print (scores[2])
 
However, that is the result get from the shell
 
Traceback (most recent call last):
  File "C:/Users/lihuiwen/Desktop/日电脑!/array1.py", line 4, in <module>
    (name.score) = line.split()
NameError: name 'name' is not defined
 
Anyone can help me solve that ?

 

 

Hello, suckingspider, I must say that please use <> button (called code tag) in the editor while pasting code. sad.png 

 

Anyway, there is slight problem in your code. Just look at your traceback and the line giving the error.

 

your code uses

(name.score) = line.split()

rather than

(name,score) = line.split()

which changes the whole meaning in python.

 

. and , have a lot different meaning when used in context of Python and can devastate your program. tongue.pnglaugh.pngcool.png

 


In Topic: Chapter 2 Code

12 July 2015 - 04:54 AM

Hi,

 

Trying to go through Ch2 and no matter which version of the program I use I keep getting this:

Traceback (most recent call last):
  File "C:\Python34\lib\urllib\request.py", line 1182, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Python34\lib\http\client.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "C:\Python34\lib\http\client.py", line 1126, in _send_request
    self.endheaders(body)
  File "C:\Python34\lib\http\client.py", line 1084, in endheaders
    self._send_output(message_body)
  File "C:\Python34\lib\http\client.py", line 922, in _send_output
    self.send(msg)
  File "C:\Python34\lib\http\client.py", line 857, in send
    self.connect()
  File "C:\Python34\lib\http\client.py", line 834, in connect
    self.timeout, self.source_address)
  File "C:\Python34\lib\socket.py", line 512, in create_connection
    raise err
  File "C:\Python34\lib\socket.py", line 503, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\Programs\Coffee Biiz.py", line 7, in <module>
    page = urllib.request.urlopen("http://beans.itcarlow.ie/prices.html")
  File "C:\Python34\lib\urllib\request.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python34\lib\urllib\request.py", line 463, in open
    response = self._open(req, data)
  File "C:\Python34\lib\urllib\request.py", line 481, in _open
    '_open', req)
  File "C:\Python34\lib\urllib\request.py", line 441, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 1210, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python34\lib\urllib\request.py", line 1184, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond>
>>> 

Anyone any ideas?

 

Thanks

Hi Damien, Check that you are connected to internet or not.

 

If you are connected; then check your proxy settings in IE or Chrome. If you are on windows (which you are on; i think from your traceback) and using LAN or Wi-Fi; it is usually "automatically detect my proxy settings" something like that.

 

In chrome, You can go to "chrome://settings/"; then click "show advance settings" and there is an option "change my proxy settings" and then change your LAN settings accordingly. :)