You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# This program prints greetings based on a user's first name and age# For a multi-line comment# Comment line 2greeting_of_the_day='welcome, adventurer!'special_greeting='welcome, your highness!'default_greeting=", howdy adventurer!"# One can use double quotes for stringsnames= ['rachel', 'jennifer', 'paul', 'james']
special_name='sarah'name=input('What is your first name? ')
age=int(input('How old are you? '))
# converting name to lowercase for comparisonname=name.lower()
ifnames.__contains__(name) andage>=21:
print(greeting_of_the_day.title())
print(name.title() +' A special drink awaits you!')
elifspecial_name.__eq__(name):
print(special_greeting.title())
print(name.title() +',A special feasts awaits you in the castle!')
elifname.startswith('a') andage>=21:
print('A drink awaits you, '+name.title() +' well done!')
else:
print(name.title() +default_greeting.title())
Java Program:
importjava.util.Arrays;
importjava.util.Scanner;
/** * This program prints greetings based on a user's first name and age */publicclassCustomGreeter {
publicstaticvoidmain(String[] args) {
vargreetingOfTheDay = "Welcome, adventurer!";
varspecialGreeting = "Welcome, your highness!";
vardefaultGreeting = ", howdy adventurer!";
varnames = newString[]{"rachel", "jennifer", "paul", "james"};
varspecialName = "sarah";
// This collects input from the user. Notice the ceremony involved with the Scanner classScannerinput = newScanner(System.in);
System.out.println("What is your first name? ");
varname = input.nextLine();
System.out.println("What is your age? ");
varage = input.nextInt();
input.close();
// Comparison to Python - a string array does not contain the 'contains' methodvarnameList = Arrays.stream(names).toList();
//General note - the String class in Java does not have a built-in toTitleCase or capitalize functionif (nameList.contains(name)&& age >= 21) {
System.out.println(greetingOfTheDay);
System.out.println(name + " , A special drink awaits you!");
}
elseif (specialName.equalsIgnoreCase(name)) {
System.out.println(specialGreeting);
System.out.println(name + ", A special feasts awaits you in the castle!");
}
else {
System.out.println(name + defaultGreeting);
}
}
}
Additional observations:
Python is concise
Java is verbose
No semicolons, curly braces, or parenthesis in Python!
Python is a layout (indentation) sensitive language
Follows Landin's offsides rule
Python follows a set of principles that focus on simplicity, readability, and beautiful code.