Reputation: 41
import time
import random
import sys
def code():
user_num=()
user_num=int(input("What number do you want from 0-30"))
if user_num>30:
print("number needs to be smaller")
print("restart code and try again")
else:
pass
if user_num<0:
print("your number needs to be greater")
print("restart code and try again")
else:
pass
code()
code()
random_num=random.randint(0,1)
if random_num==user_num:
print("your number is correct")
else:
print("your number is incorrect")
time.sleep(1)
try_again=input("do you want to try again (yes/no")
if try_again=="yes":
code()
else:
print("ok. Bye")
i am very new to functions so sorry if this is a rookie mistake. Any help with functions will be appreciated. Thank You.
Upvotes: 0
Views: 927
Reputation: 4100
Try this:
import time
import random
import sys
def code():
user_num=()
user_num=int(input("What number do you want from 0-30"))
if user_num>30:
print("number needs to be smaller")
print("restart code and try again")
else:
pass
if user_num<0:
print("your number needs to be greater")
print("restart code and try again")
else:
pass
return user_num
random_num=random.randint(0,1)
user_num = code()
if random_num==user_num:
print("your number is correct")
else:
print("your number is incorrect")
time.sleep(1)
try_again=input("do you want to try again (yes/no")
if try_again in "yes":
user_num = code()
else:
print("ok. Bye")
Upvotes: 0