We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,578 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

novice python question

so im trying to make it so that pressing enter without inputting any value, will break the loop, however, an empty string can be used because of

for i in range(len(x_str_array)):
x[i] = int(x_str_array[i]) bit.
'Exercise 7.3'

# modified program from exercise 7.2

a=1
z=""
while x !=z:
x_str = raw_input('Enter Start,Stop,Increment: ')
if x==z:
break
else:
x_str_array = x_str.split(',') # splits the string where a comma pops up
x = range(len(x_str_array)) # Preallocation step- makes x= the same length as x_str_array
for i in range(len(x_str_array)):
x[i] = int(x_str_array[i])
a=x[0]
b=x[1]
c=x[2]
for number in range(a,b,c):
print "number:", number, " square:", number**2, " cube:", number**3

please advise me on this.

p.s yes i am a complete novice

i thought maybe using an empty tuple or list or something, but that wouldn't work either.

3
Contributors
4
Replies
7 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
Question
Answered
pwolf
Junior Poster
106 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

To break the loop on empty input, use

while True:
    x_str = raw_input('Enter Start,Stop,Increment: ')
    if not x_str:
        break
    else:
        ...

edit: where is indention in your code ? Configure your editor to indent with 4 spaces when you hit the tab key.

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

To break the loop on empty input, use

while True:
    x_str = raw_input('Enter Start,Stop,Increment: ')
    if not x_str:
        break
    else:
        ...

edit: where is indention in your code ? Configure your editor to indent with 4 spaces when you hit the tab key.

thank you very much Gribouillis, i have been stuck on this for ages. im very grateful!

pwolf
Junior Poster
106 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Gribouillis

Your code indention is completely lost.

pyTony
pyMod
Moderator
6,330 posts since Apr 2010
Reputation Points: 879
Solved Threads: 989
Skill Endorsements: 27

Your code indention is completely lost.

yeah, i know, i posted the code onto a forum earlier and lost the indentation. also didnt' get any replies. sorry about that

pwolf
Junior Poster
106 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0648 seconds using 2.65MB