# how to make loop in a if statement

Page 1 of 1

## 12 Replies - 1486 Views - Last Post: 26 November 2012 - 12:52 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=300386&amp;s=854241389c565dce28fe27c381fe88c0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 littlePilot

Reputation: 2
• Posts: 23
• Joined: 07-March 12

# how to make loop in a if statement

Posted 15 November 2012 - 04:57 PM

hi every one lol... i was wondring is someone could help me to get this question to loop while its inside an if statment

```def D_or_P():
pass
while True:
try:
d_or_p =input("Will this order Be Delivery? (yes or no): ").lower()
if d_or_p == 'yes':
d_or_p= 3
break
elif d_or_p == 'no':
d_or_p= 0
break
except ValueError:
pass
print ("Please Enter a Yes or No")

if d_or_p in (range (3,4)):
PH = int(input("whats your phone number: "))
```

i want to try to get PH to make sure a int is enterd but i need to to be in the if stament so only is d_or_p = yes it is activated and also is it possabal to get A to loop if less then 2 letters are entred. thax

Is This A Good Question/Topic? 0

## Replies To: how to make loop in a if statement

### #2 jon.kiparsky

• Pancakes!

Reputation: 9537
• Posts: 16,482
• Joined: 19-March 11

## Re: how to make loop in a if statement

Posted 15 November 2012 - 05:30 PM

```question_not_answered = True

```

This works for me. I guess you're using py3 so use input instead.

### #3 Acidogenic

Reputation: 1
• Posts: 5
• Joined: 07-July 12

## Re: how to make loop in a if statement

Posted 15 November 2012 - 05:35 PM

```question_not_answered = True
else:
print('Error: Not a Number')

```

Just a little touchup to the code Jon wrote to tell the user what is wrong.

### #4 jon.kiparsky

• Pancakes!

Reputation: 9537
• Posts: 16,482
• Joined: 19-March 11

## Re: how to make loop in a if statement

Posted 15 November 2012 - 05:49 PM

Probably "Error: 'yes' or 'no'" would be more appropriate, but yes.

I'm not sure what the point of the conversion from 'yes' to 3 was, but you can do that after you have a value, if it's actually necessary.

### #5 littlePilot

Reputation: 2
• Posts: 23
• Joined: 07-March 12

## Re: how to make loop in a if statement

Posted 15 November 2012 - 06:58 PM

jon.kiparsky, on 15 November 2012 - 05:49 PM, said:

Probably "Error: 'yes' or 'no'" would be more appropriate, but yes.

I'm not sure what the point of the conversion from 'yes' to 3 was, but you can do that after you have a value, if it's actually necessary.

i only change it because i use it in math later so its fine how it is.

and i get how it works but i still don't under stand how i am meant to us that to loop the phone number in the if state meant because i need something to make sure that it = a number then continues....

but i have to say i like your loops a lot better then the ones im using coz they make a lot more seance but i need to figure out how to go
if PH = int: then its false.
if PH != int: then its still true

but i don't know what to put instead of int to check

### #6 Tayacan

• D.I.C Regular

Reputation: 145
• Posts: 275
• Joined: 18-January 11

## Re: how to make loop in a if statement

Posted 16 November 2012 - 05:17 AM

You convert a string to an int like this:
```int(s)
```

Assuming that s is a string containing only digits. If not, it'll throw an exception. There are several ways of dealing with that. You can use a try-except block, of course, or you can check in advance if the string contains only digits.

And yes, you'll need a loop inside your if statement. You do that by... putting the loop inside the if statement. Like this:
```if(some_condition):
while(some_other_condition):
do_stuff

```

### #7 littlePilot

Reputation: 2
• Posts: 23
• Joined: 07-March 12

## Re: how to make loop in a if statement

Posted 18 November 2012 - 01:39 AM

Tayacan, on 16 November 2012 - 05:17 AM, said:

You convert a string to an int like this:
```int(s)
```

Assuming that s is a string containing only digits. If not, it'll throw an exception. There are several ways of dealing with that. You can use a try-except block, of course, or you can check in advance if the string contains only digits.

And yes, you'll need a loop inside your if statement. You do that by... putting the loop inside the if statement. Like this:
```if(some_condition):
while(some_other_condition):
do_stuff

```

is it possible to ask for a example of this in use with a number as the variable that needs loop? O.o

### #8 jon.kiparsky

• Pancakes!

Reputation: 9537
• Posts: 16,482
• Joined: 19-March 11

## Re: how to make loop in a if statement

Posted 18 November 2012 - 01:46 AM

As it turns out, str.isdigit returns true if the characters of the string are all digits.
Who knew? Not me, until I tried.

```>>> "123412341513".isdigit()
True
>>>
```

Does that help?

### #9 Tayacan

• D.I.C Regular

Reputation: 145
• Posts: 275
• Joined: 18-January 11

## Re: how to make loop in a if statement

Posted 18 November 2012 - 10:12 AM

Alright, here's what I think you asked for.

First, we set PH to something that is definitely not a phone number. Then, as long as PH has that value, we ask the user for a phone number. As soon as we get something that is a valid phone number, we give PH that value, hence PH will no longer be equal to -1, and the loop will stop.
```if d_or_p in (range (3,4)):
PH = -1
while PH == -1:
# some code here that asks for a phone number,
# ensuring that it is indeed a number. If it is,
# we should change PH to the phone number.

```

### #10 littlePilot

Reputation: 2
• Posts: 23
• Joined: 07-March 12

## Re: how to make loop in a if statement

Posted 19 November 2012 - 02:05 AM

Tayacan, on 18 November 2012 - 10:12 AM, said:

Alright, here's what I think you asked for.

First, we set PH to something that is definitely not a phone number. Then, as long as PH has that value, we ask the user for a phone number. As soon as we get something that is a valid phone number, we give PH that value, hence PH will no longer be equal to -1, and the loop will stop.
```if d_or_p in (range (3,4)):
PH = -1
while PH == -1:
# some code here that asks for a phone number,
# ensuring that it is indeed a number. If it is,
# we should change PH to the phone number.

```

```d_or_p = 3

if d_or_p in (range (3,4)):
PH = -1
while PH == -1:
blaa = (input ("what is your phone number?"))

if (blaa in str(range(0,99999999999))):
PH = blaa
elif (blaa not in str(range(0,99999999999))):
print("noooo")

print ("woot")

```

this is what i have done but the only problem is it only exits the loop when 0 or 99999999999 is entered anything else it just loops and i dont under stand why.... can you explain to me why that is please?

### #11 Tayacan

• D.I.C Regular

Reputation: 145
• Posts: 275
• Joined: 18-January 11

## Re: how to make loop in a if statement

Posted 20 November 2012 - 02:03 AM

If I try to run your code, I get the following:
```Traceback (most recent call last):
File "ph.py", line 8, in <module>
if (blaa in str(range(0,99999999999))):
OverflowError: range() result has too many items

```

Really, making a range of 99999999999 items might not be the best idea ever. Instead, you can do this:
```if len(str(blaa)) in range(12) and blaa >= 0:
```

Your elif can be replaced by an else.

### #12 littlePilot

Reputation: 2
• Posts: 23
• Joined: 07-March 12

## Re: how to make loop in a if statement

Posted 26 November 2012 - 12:28 AM

i tryed to use that but it didnt work....

### #13 littlePilot

Reputation: 2
• Posts: 23
• Joined: 07-March 12

## Re: how to make loop in a if statement

Posted 26 November 2012 - 12:52 AM

```d_or_p = 3

if d_or_p in (range (3,4)):
PH = -1
while PH == -1:
blaa = int(input ("what is your phone number?"))
if len(str(blaa)) in range(12) and blaa >= 0:
PH = 1
else:
print ("Please enter number under 5")

print ("woot")
```