1
my program confuse between integer value and string 
import random


def test():
    num=random.randrange(100,1000,45)
    while True:
        ans=run(num)
        print ans
        ss=raw_input("if you want to exit press t: ")
        if ss=='t':
            break

def run(userinput2):
    first = int(userinput2/20)
    print "i will send it in %s big pack"%str(first)
    userinput2=userinput2%20
    second =int(userinput2/10)
    print "i will send it in %s med pack"%str(second)
    third =userinput2%10
    print "i will send it in %s med pack"%str(third)



def main():
    print "the began of pro"
    print "@"*20
    userinput=raw_input("test or run: ")
    if userinput.lower()=='test':
        test()
    else:
        while True:
            userinput2=int(raw_input("press t to exit or chose a number:"))
            if userinput2 =='t':
                break
            else:
                answer=run(userinput2)


if __name__ == "__main__":
      main()

this piece of code i has error in it

userinput2=int(raw_input("press t to exit or chose a number:")) if userinput2 =='t':

if i change it to string i had it not accept string and if make it string it not accept integers

2
  • Why not press -1 to exit or choose a number? That saves you the problem of deciding if the input should be string and/or castable to int Commented Jul 26, 2016 at 0:17
  • my task said to press t only Commented Jul 26, 2016 at 0:27

1 Answer 1

2

I think that this covers the cases you need:

while True:
    userinput2=raw_input("press t to exit or chose a number:")
    if userinput2 =='t':
        break
    try:
        userinput2 = int(userinput2)
    except ValueError:
        print('That was neither a number nor a "t".  Try again.')
        continue
    answer=run(userinput2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.