12 Replies - 844 Views - Last Post: 26 November 2012 - 12:52 AM Rate Topic: -----

#1 littlePilot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • 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: "))
    A = input("what is your adress: ")


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  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,203
  • Joined: 19-March 11

Re: how to make loop in a if statement

Posted 15 November 2012 - 05:30 PM

question_not_answered = True
while (question_not_answered):
  answer = input("Question?")
  if (answer == 'yes' or answer == 'no'):
    question_not_answered = False



This works for me. I guess you're using py3 so use input instead.
Was This Post Helpful? 0
  • +
  • -

#3 Acidogenic  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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
while (question_not_answered):
  answer = input("Question?")
  if (answer == 'yes' or answer == 'no'):
    question_not_answered = False
  else:
     print('Error: Not a Number')



Just a little touchup to the code Jon wrote to tell the user what is wrong.
Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,203
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 littlePilot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 07-March 12

Re: how to make loop in a if statement

Posted 15 November 2012 - 06:58 PM

View Postjon.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
Was This Post Helpful? 0
  • +
  • -

#6 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • 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


Was This Post Helpful? 1
  • +
  • -

#7 littlePilot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 07-March 12

Re: how to make loop in a if statement

Posted 18 November 2012 - 01:39 AM

View PostTayacan, 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
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,203
  • 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?
Was This Post Helpful? 0
  • +
  • -

#9 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • 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.


Was This Post Helpful? 0
  • +
  • -

#10 littlePilot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 07-March 12

Re: how to make loop in a if statement

Posted 19 November 2012 - 02:05 AM

View PostTayacan, 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? :)
Was This Post Helpful? 0
  • +
  • -

#11 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 littlePilot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • 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....
Was This Post Helpful? 0
  • +
  • -

#13 littlePilot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • 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")


how do i get it to handle the error? please help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1