-3
import random 

print ("hello")

user_friend1 = raw_input("Name one of your friends: ")

user_friend2 = raw_input("Name another friend: ")
# inputs for user
friends = [user_friend1,user_friend2] 

best_friend = random.sample(friends, 1)

I want to loop it so that if a user types an integer to a question then it will ask the question again, i need it for both questions user_friend 1 and 2

print ("your best friend is %s") % (best_friend) 
ForceBru
  • 43,482
  • 10
  • 63
  • 98

3 Answers3

1
if type(variable) == int:

This gets the type of the variable and compares it to the class int

Andria
  • 4,712
  • 2
  • 22
  • 38
0

You should use isinstance()

if isinstance(variable, int):
    do_something()
cmeadows
  • 185
  • 2
  • 12
0

Use this

import random
while True:
    friend1 = raw_input("Name one of your friends: ")
    try:
        val = int(friend1)
    except ValueError:
        break
    print("Enter valid name")
while True:
    friend2 = raw_input("Name another friend: ")
    try:
        val = int(friend2)
    except ValueError:
        break
    print("Enter valid name")
friends = [friend1,friend2]
best_friend = random.sample(friends, 1)
print("your bestfriend is %s" %(best_friend))

Output

Name one of your friends: 1
Enter valid name

Name one of your friends: 1
Enter valid name

Name one of your friends: tilak

Name another friend: varma
your bestfriend is ['varma']
Tilak Putta
  • 758
  • 4
  • 18