PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 22, 2022

[FIXED] How do I get my (multiple) conditions to be met?

 November 22, 2022     if-statement, multiple-conditions, python-3.9     No comments   

Issue

so recently I've just been working on a survey with if else statements and ran into the issue of my conditions not fully being met. At first it seemed as if my code was running well, but then I realized that with certain inputs the code would crumble or respond to a different condition.

My biggest issue is when I type in any in (2 through 10) it refers to the condition that should only be possible if the input is greater than 18... however numbers (10 through 17) work perfectly fine for what I need

Likewise with any number 100 and over it does not refer to the >18 code but instead to the less <18

I feel like im missing something, here is the code below, pls help

print("Welcome user, today we will be evaluating if you need to sign up for selective service or not.\n"
      "DO NOT LIE!")
print("")
name = input("Please enter your name: ")
age = input("Please enter your age: ")
Male = "M".casefold()
Female = "F".casefold()
gender = input("Please enter an (M or F) for gender identity: ").casefold()

if age >= str(18) and gender == Male:
    print("Welcome {}, since you are a Male that is 18 or over please sign where directed for selective service".format(name))
    input("Name and DOB: ")
    print("You will get a letter/email if your service is ever required.")

if age < str(18)) and gender == Male:
    print("You are not required to signup for selective service yet {}, please return in {} years.".format(name, 18-int(age)))

else:
    if gender == Female:
        print("You do not meet the criteria to sign up, have a good day!")

Solution

You have taken string input and comparing it with str(18). Which is comparing two strings rather than two numbers. so when you do something like "9" < "18", you are actually comparing their Unicode values rather the numbers themselves. So rather that converting integers to string convert your input string to integers, so your code should be something like this:

print("Welcome user, today we will be evaluating if you need to sign up for selective service or not.\n"
      "DO NOT LIE!")
print("")
name = input("Please enter your name: ")
age = input("Please enter your age: ")
Male = "M".casefold()
Female = "F".casefold()
gender = input("Please enter an (M or F) for gender identity: ").casefold()

if int(age) >= 18 and gender == Male:
    print("Welcome {}, since you are a Male that is 18 or over please sign where directed for selective service".format(name))
    input("Name and DOB: ")
    print("You will get a letter/email if your service is ever required.")

if int(age) < 18 and gender == Male:
    print("You are not required to signup for selective service yet {}, please return in {} years.".format(name, 18-int(age)))

else:
    if gender == Female:
        print("You do not meet the criteria to sign up, have a good day!")


Answered By - Suryansh Singh
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing