0
# This works until line 7
sex = input("Are you male(m) or female(f)? ")
me = 75 * 52
fe = 80 * 52
ca = input("What is your current age in years? ") * 52
print(sex)  # This is returning the input from the first line.
if sex == m:
    print(f"You have approximately {me - ca} weeks to live. ")
else:
    print(f"You have approximately {fe - ca} weeks to live. ")

The variable is assigned a string, but it's not working as a Boolean in the if statement.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eyeclick
  • 11
  • 1
  • The problem is `ca = input("What is your current age in years? ") * 52`. `input()` returns str. Print `ca` to see what you work with – buran Jun 04 '23 at 19:20
  • 1
    I think you want `sex == 'm'`. – rassar Jun 04 '23 at 19:20
  • 2
    `if sex == m` compares `sex` to a **variable** named `m`. If you want to compare to a literal string it's `if sex == 'm'`. – slothrop Jun 04 '23 at 19:20
  • 1
    Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – buran Jun 04 '23 at 19:20
  • 1
    @buran That's another problem for sure, but OP first has to get past the hurdle of *"it's not working as a boolean in the if statement"* – slothrop Jun 04 '23 at 19:21
  • 1
    @slothrop, indeed. But that was first error that I saw :-) – buran Jun 04 '23 at 19:22
  • Your assignment to `ca` isn't going to do what you think, either. Since `input` returns a string, `ca` is the result of multiple string concatenations. For example, `'5'* 52` is not `260`, but `'5555555555555555555555555555555555555555555555555555`. (That is, the string `'5'` repeated 52 times.) – chepner Jun 04 '23 at 19:23

2 Answers2

0

There are two problems with this code.

  1. sex == m is comparing sex, a variable to m, another variable that was not defined. you need to compare sex to 'm', a string.

  2. input() returns a string. you need to convert that string into an integer before multiplying by 52

Here is a correct version for OP's code:

sex = input("Are you male(m) or female(f)? ")
me = 75 * 52
fe = 80 * 52
ca = int(input("What is your current age in years? ")) * 52
print(sex)  #  this is returning the input from the first line.
if sex == 'm':
    print(f"You have approximately {me - ca} weeks to live. ")
else:
    print(f"You have approximately {fe - ca} weeks to live. ")
Eduardo
  • 697
  • 8
  • 26
someone
  • 122
  • 4
-1

sex == m, python will understand this as variable sex = variable m

The auto type of the input is string, you need to change it to int by int().

Here is my code after fixing:

sex = input("Are you male(m) or female(f)? ")
me = 75 * 52
fe = 80 * 52
ca = int(input("What is your current age in years? ")) * 52
print(sex)  # This is returning the input from the first line.
if sex == "m":
    print(f"You have approximately {me - ca} weeks to live. ")
else:
    print(f"You have approximately {fe - ca} weeks to live. ")