I want to make a program that outputs something like:

Enter integer: 117
117 is lucky!

Enter integer: 1003
1003 is not lucky!

So, if the integer entered contains at least one 7 in it, it will output is lucky! And if it doesn't it will output the is not lucky!

Here's my code:

lucky=(raw_input("Enter a integer: ")


for i in range(len(lucky)):
    if lucky[i] == "7":
        print str(lucky) + " " + "is lucky!"
    else:
        print str(lucky) + " " + "is not lucky."

I don't really know how to fix the code to output what I want it to, any help appreciated, thanks.

You could write

if "7" in lucky:
    print lucky + " " + "is lucky!"
else:
    print lucky + " " + "is not lucky."

Also you could add some code to check if the input was an integer

try:
    int(lucky.strip())
except ValueError:
    print("Error: an integer is required.")

I never learned the "in" function yet. What if I wanted to use the index code in my code? Like the lucky, is there a way to do it? I haven't learned much about python yet, so I wanted to see if I can use what I've learned to finish this code first.

(learned the index, for loops, while loops and if statement)
Thanks.

I never learned the "in" function yet. What if I wanted to use the index code in my code? Like the lucky, is there a way to do it? I haven't learned much about python yet, so I wanted to see if I can use what I've learned to finish this code first.

(learned the index, for loops, while loops and if statement)
Thanks.

You should remove the parenthesis from around your input statement. so it should just be: lucky = raw_input("Enter an integer: ") The rest will work except that it will print a message on every character that is evaluated. You should be using a boolean variable that is initially set to false before your loop. If the number 7 is found, change it to True, otherwise do nothing.

After the loop has completed, if your boolean is true, print the lucky message; otherwise print unlucky.

HTH

I don't really get what you mean by initially setting a boolean variable to false before the loop. Do you think you can give me an example?

Also for the for loop, should I get rid of it? Because if I enter 007, it will print the statements 3 times and I only want it to print once.

I don't really get what you mean by initially setting a boolean variable to false before the loop.

Like this: my_boolean = False . Now the variable named my_boolean will act as your "flag" to tell you whether a number is "lucky" or not.

Also for the for loop, should I get rid of it? Because if I enter 007, it will print the statements 3 times and I only want it to print once.

Here's what I posted above, maybe you didn't read it last time:

The rest will work except that it will print a message on every character that is evaluated. You should be using a boolean variable that is initially set to false before your loop. If the number 7 is found, change it (the boolean) to True, otherwise do nothing. [EDIT: Meaning this should be the only logic in your for loop. Take everything else out]

After the loop has completed, check if your boolean is true; if so, print the lucky message or otherwise print unlucky.

HTH

Try this:

is_lucky = False  # a boolean value set to False
lucky = raw_input("Please enter an integer: ")  # Notice only one set of parenthesis
for i in range(len(lucky)):
    if lucky[i] == "7":
        is_lucky = True
    else:
        pass
if is_lucky:
    print "%d is lucky!" % int(lucky)
else:
    print "%d is not lucky!" % int(lucky)

This is just on the fly, but I tested it and it works.

This is just on the fly, but I tested it and it works.

Yes it work ,but this part It`s making ting a little harder and dont make much sense

for i in range(len(lucky)):
    if lucky[i] == "7":

.

>>> lucky = raw_input("Please enter an integer: ")
Please enter an integer: 7
>>> lucky
'7'
>>> #now why should we use and string in a range for loop.
>>> #dos len make it better
>>> len(lucky)
1
>>> #no
>>> for i in range(1):
	i
	
0
>>> #this line is much clearer
>>> if '7' in lucky:
>>> #dos the same as this lines
>>> for i in range(len(lucky)):
    if lucky[i] == "7":
        is_lucky = True
>>>

So if we put suggestion in this poste together with a loop an exception handling.
It would look somthing like this.

while True:   
    try:
        lucky = raw_input("Enter an integer: ")
        int(lucky.strip())
        if '7' in lucky:
            print '%s is lucky!' % (lucky)
            #break #if you want out of the loop
        else:
            print '%s is not so lucky' % (lucky)
    except ValueError:
        print 'Please number only'

I never learned the "in" function yet. What if I wanted to use the index code in my code? Like the lucky, is there a way to do it? I haven't learned much about python yet, so I wanted to see if I can use what I've learned to finish this code first.

(learned the index, for loops, while loops and if statement)
Thanks.

You are correct that a cleaner and simpler code would use "if in", but the thread owner stated that he wanted to use indexing, so I gave an example using len and range to iterate through each digit looking for "7".

These little projects are fun. You can make minor adjustments to the OP's original code and make it work ...

lucky = raw_input("Enter an integer: ")

for c in lucky:
    if c == "7":
        print lucky + " is lucky!"
        break
else:
    print lucky + " is not lucky."

The loop iterates through the lucky number string character by character. Also note that the else goes with for (not the if). This way else goes into action only if there is no loop break, which implies that there is no '7' found.

Another way

lucky = raw_input("Enter an integer: ")
x = "" if lucky.count("7") else "not "
print("%s is %slucky" %(lucky, x))

Wow, all this to avoid in :)

Member Avatar for sravan953

Try:

lucky=input("Enter a number: ")

while True:
    n=lucky%10
    if(n=='7'):
        print(str(lucky)+" is lucky!")
        return False
    lucky=lucky/10

Try:

lucky=input("Enter a number: ")

while True:
    n=lucky%10
    if(n=='7'):
        print(str(lucky)+" is lucky!")
        return False
    lucky=lucky/10

Make some corrections and it will work with Python2 ...

lucky=input("Enter a number: ")

while True:
    n = lucky % 10
    if  n == 7:
        print(str(lucky)+" is lucky!")
        break
    lucky = lucky/10  # better to use lucky = lucky//10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.