Reputation: 13
I need a bit of help. I don't understand what I am doing wrong. I need code that checks the first number of user input. For example I enter the numbers 34566
and it looks at the first number and prints 'he' or 'she'.
Maybe some one can give me some advice how to solve it.
print("Sisesta isikukood")
isikukood[0] = int(input())
if (isikukood[0] == 1 or isikukood[0]== 3 or isikukood[0]== 5):
print("He")
else:
if isikukood[0] == 2 or isikukood[0]== 4 or isikukood[0] == 6:
print("She")
else:
print("Vale isikukood")
Upvotes: 1
Views: 3512
Reputation: 181
Get the input as a string. Then get the first number of that input like this isikukood[:1]. As the first number is now a string (not integer) you should compare it with a string.
print("Sisesta isikukood")
isikukood = str(input())
if (isikukood[:1] == '1' or isikukood[:1] == '3' or isikukood[:1] == '5'):
print("He")
elif isikukood[:1] == '2' or isikukood[:1]== '4' or isikukood[:1] == '6':
print("She")
else:
print("Vale isikukood")
Edu :)
Upvotes: 0
Reputation: 369124
Instead of convert the string (the value returned by input()
), leave it as a string. And you can use index operator to get the first character:
print("Sisesta isikukood")
num = input() # no `int(..)`
if num[:1] in ('1', '3', '5'):
print("He")
elif num[:1] in ('2', '4', '6'):
print("She")
else:
print("Vale isikukood")
NOTE: The characters should be compared with characters. ('1', '3', '5', ...
instead of 1, 3, 5
)
Upvotes: 1