Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Python 3 vs Python 2.7.3 Problem

python 3 python 2.7.3 user input iteration variables

Best Answer Flying Dutchman, 13 March 2013 - 04:22 PM

fields = ["name", "email", "phone"]
answers= []

for field in fields:
    answers.append(input("What is your %s? " % field))
	
for field, answer in zip(fields, answers):
    print("Your %s is %s" % (field, answer))

Works in Python 3.2.3

 

In your 3.x code, why are you using input(field) instead of print(field) ? input pretty much does the same as your prompt.

 

Anyways, your 2.7 code works just fine in Python 3, you just forgot parenthesis around 2nd print.

print ("""Hello %s!
Your Email is: %s
Your Phone is: %s""" % ( answers[0], answers[1], answers[2] ))
Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 elcentro3m

elcentro3m

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 12 March 2013 - 08:00 PM

import sys
def prompt():
    response = sys.stdin.readline().strip()
    return response
fields = [ "Name: ", "Email: ", "Phone: " ]
answers = []
for field in fields:
    print (field),
    v = prompt()
    answers += [v]
print """Hello %s!
Your Email is: %s
Your Phone is: %s""" % ( answers[0], answers[1], answers[2] )

 

Works in Idle Python 2.7.3

#!/usr/bin/env python
# iterating through an array

import sys
def prompt():
    response = sys.stdin.readline().strip()
    return response

arr = ("Name: ", "Email: ", "Phone: ")
test = []
 
for field in arr:
   input(field),
   v = prompt()
   test += [v]

print """Hello %s!
Your Email is: %s
Your Phone is: %s""" % ( test[0], test[1], test[2] )

 

Not working.

I'm VERY new to Python, but, not to coding.  As best I can tell, it:

1) Uses a for loop to cycle through the prompts via an array

2) Assigns the answers using the response via variable "v" and
3) Prints the answers out

 

My biggest headache in all of this is working through the differences between the different versions of Python.  Used to have the same headache working through the different versions of Flash Actionscript.
Example: raw_input vs input (v2.7.3 vs 3.2)

I've looked through several sites.  Given the variables at work here: user input, iteration, python 3 vs
2.7.3, arrays, for loop.  I haven't found anything which addresses the specific components of this code or within that the differences between 2.7.3 and 3.2 Python.

 

I could continue to stumble fumble around for info on this.  I find information from unrelated to sort of related but not specific to this particular coding example, to wildly off the mark of no use.

I've figured out how to do it with simple inputs:

 

#Python 3.2
#mult_input_test.py

print("");
name = input("What is your name? ")
email = input("What is your email address? ")
phone = input("What is your phone number? ")
print("");
print("Your name is:", name);
print("Your email address is:", email);
print("Your phone number is:", phone);
print("");

 


But, while it works, it seems so much less elegant than the original.

 

Anyone got any ideas?

 

Thanx



#2 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts

Posted 13 March 2013 - 04:22 PM   Best Answer

fields = ["name", "email", "phone"]
answers= []

for field in fields:
    answers.append(input("What is your %s? " % field))
	
for field, answer in zip(fields, answers):
    print("Your %s is %s" % (field, answer))

Works in Python 3.2.3

 

In your 3.x code, why are you using input(field) instead of print(field) ? input pretty much does the same as your prompt.

 

Anyways, your 2.7 code works just fine in Python 3, you just forgot parenthesis around 2nd print.

print ("""Hello %s!
Your Email is: %s
Your Phone is: %s""" % ( answers[0], answers[1], answers[2] ))

The roots of education are bitter, but the fruit is sweet.


#3 elcentro3m

elcentro3m

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 16 March 2013 - 10:00 AM

Thank you very much for this.

 

Why did I do what I did?

 

Simple.

 

I'm a beginner.

 

You, are a "God".

 

That's the simple difference.

 

Thanx



#4 elcentro3m

elcentro3m

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 16 March 2013 - 10:33 AM

Yours is a simple, equally elegant answer, Flying Dutchman.

 

I got the original code here:
 

http://www.daniweb.c...input-in-python

 

Next challenge for me (no small one) to look at ultimately developing this to:


Open file
 

Prompts (R/ead, A/dd, L/ist all, P/rint, W/rite, Q/uit)

While != 'q"


Read - Read name, email address, phone for Name(User Input)

 

or

Add - Add new name, email address, phone until Q (Write? (Y/N)

 

or

 

L/ist - Show all entries format: name, email address, phone

 

or

 

P/rint - Print formatted list: format: name, email address, phone

 

or

Quit - If Add, prompt for Write, otherwise Quit

 

Seems like an accessible challenge for learning to code in Python.

 

That's my goal.

 

This is just the stepping off point.

 

Haven't really looked While Loops yet, if/then loops or file I/O.

 

I've seen some stuff.

 

Any recommendations?

 

Again, Thanx.



Question.  Changing "zip to dip" breaks the program
error "dip" is not defined.

 

"zip" isn't defined either:

" for field, answer in zip(fields, answers): "

 

Is it an internal function to python or what am I missing?



Nevermind.  Search is my friend:

"This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length, zip is similar to map with an initial argument of None. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list."

Now to look up "tuples".



#5 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts

Posted 16 March 2013 - 12:10 PM

Heh, I ought to change my "title", since it's based on number of posts and not the actual skill. :P

 

So, you want to make a phone book. That's pretty common assignment and it most certainly can be done with Python. I think the best thing would be for you to post ideas/code and we'll discuss it then.

 

Tuples are like lists, except then can't be modified. I believe the correct term is that they're immutable. zip is indeed an internal function, but you can easily override it, if you want to.


The roots of education are bitter, but the fruit is sweet.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download