In: Computer Science
e. Task 5 (3 pts): (This task uses Strings and an if..then..elseif cascade) A program that prompts the user for their party affiliation (Democrat, Republican, or Independent) and responds accordingly with a Donkey,
SOLUTION IN JAVA:
import java.util.Scanner;
class Main
{
public static void main(String[] args) {
// creating scanner object for taking input
Scanner scan = new Scanner(System.in);
// taking user input
System.out.print("Enter your party affiliation: ");
String party_name = scan.next();
// if-then-else cascading - what it means basically is
// else part of each if is nested until all conditions are checked
if(party_name.equalsIgnoreCase("Republican")){
System.out.println("Elephant");
}
else{
if(party_name.equalsIgnoreCase("Independent")){
System.out.println("Eagle");
}
else{
System.out.println("Donkey");
}
}
}
}
SAMPLE OUTPUT:
SOLUTION IN PYTHON:
def main():
# asking user for input
party_name = input('Enter your party
affiliation: ')
# if-then-else cascading
if party_name.lower() == 'republican':
print('Elephant')
else:
if party_name.lower() ==
'independent':
print('Eagle')
else:
print('Donkey')
if __name__ == '__main__':
main()
SCREENSHOT :