2

I was trying to make a simple survey bot, but I had trouble with the second if statement. it just ignores the if and elif, and goes straight to the else statement. I have tried everything, and even though it's probably going to be an easy solution please help...

 import sys

 yes = "yes"
 no = "no"
 experience_good = "yes"
 contact = "1"

 print("How many times have you bean contacted by us in the past quarter (3 months)")
 contact = sys.stdin.readline()

 if contact != 0:
     print("Was your experience was us good? Type a 1 for yes or 0 for no")
     experience_good = sys.stdin.readline()
     print("experiencence_good is", experience_good)
     # The above line was just to double check the variable was inputted 
correctly
     if experience_good is 1:
         print("Good to hear that.")
     elif experience_good is 0:
         print("Sorry about that. What could we be doing better?")
         doing_better = sys.stdin.readline()
     else:
         print("Stuff's been messed up")

The output I get is just:

How many times have you bean contacted by us in the past quarter (3 months)

3

Was your experience was us good? Type a 1 for yes or 0 for no

1

exp_good is 1

Stuff's been messed up

Community
  • 1
  • 1
Emerson SD
  • 23
  • 3

3 Answers3

1

because experience_good is never 1! It's starts off as

experience_good = "yes"

Then halfway through it maybe changed as

experience_good = sys.stdin.readline()

At this point if the user typed in 1, what the variable will hold is the string value '1' which is not 1 so you need to

experience_good = int(sys.stdin.readline().strip())
e4c5
  • 52,766
  • 11
  • 101
  • 134
1

You must use == for int equality test:

if contact != 0:
    print("Was your experience was us good? Type a 1 for yes or 0 for no")
    experience_good = sys.stdin.readline()
    print("experiencence_good is", experience_good)
    # The above line was just to double check the variable was inputted  correctly
    if experience_good == 1:
        print("Good to hear that.")
    elif experience_good == 0:
        print("Sorry about that. What could we be doing better?")
        doing_better = sys.stdin.readline()
    else:
        print("Stuff's been messed up")

Cast the contact and experience_good to int:

contact = int(contact)

experience_good = int(experience_good)
Szabolcs Dombi
  • 5,493
  • 3
  • 39
  • 71
1

Indentation problem (now edited in question). Bring the last elif and last else block one indent backwards.

if experience_good == 1:
    print("Good to hear that.")
elif experience_good == 0:
    print("Sorry about that. What could we be doing better?")
    doing_better = sys.stdin.readline()
else:
    print("Stuff's been messed up")

Use == to evaluate a value. When you use 'is' it will compare the objects. Not it's value.

Bishwas Mishra
  • 1,235
  • 1
  • 12
  • 25