HackerRank 30-days Python

This post only records the problems I made mistakes.

Intro to Conditional Statements

Task: Given an integer,n, perform the following conditional actions:

  • If is odd, print Weird
  • If is even and in the inclusive range of to , print Not Weird
  • If is even and in the inclusive range of to , print Weird
  • If is even and greater than , print Not Weird

Complete the stub code provided in your editor to print whether or not is weird.

1
2
3
4
5
6
if __name__ == '__main__':
n = int(input().strip())
if n%2==0 and (n in range (2,6) or n>20):
print('Not Weird')
if n%2==1 or (n%2==0 and (n in range(6,21))):
print('Weird')

Note: When think about the condition, we can simply catergorize the condition and try whether we can put them in one statement, which will make the codes cleaner.

Class vs. Instance

Task: Write a Person class with an instance variable,age *,and a constructor that takes an integer,initialAge* , as a parameter. The constructor must assign *initialAge* to age after confirming the argument passed as *initialAge* is not negative; if a negative argument is passed as *initialAge*, the constructor should set *age*** to 0 and print Age is not valid, setting age to 0.. In addition, you must write the following instance methods:

  1. yearPasses() should increase the instance variable by 1.
  2. amIOld() should perform the following conditional actions:
    • If age < 13 , print You are young..
    • If age $\geq$ 13 and age < 18, print You are a teenager..
    • Otherwise, print You are old..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Person:
age = 0
def __init__(self,initialAge):
# Add some more code to run some checks on initialAge
if initialAge < 0:
print ("Age is not valid, setting age to 0.")
else:
self.age = initialAge
def amIOld(self):
# Do some computations in here and print out the correct statement to the console
if self.age < 13:
print ("You are young.")
elif self.age >= 13 and self.age < 18:
print ("You are a teenager.")
else:
print ("You are old.")
def yearPasses(self):
# Increment the age of the person in here
       self.age += 1

Loops

Task: Given an integer, , print its first multiples. Each multiple (where ) should be printed on a new line in the form: n x i = result.

input Format: A single integer,n

Constraints: 2 $\leq$ n $\leq$ 20

Output Format:

Print 10 lines of output; each line i (where 1 \leq i \leq 10) contains the result of n x i in the form:
n x i = result

Sample Input: 2

Sample Output:

1
2
3
4
5
6
7
8
9
10
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
1
2
3
4
5
if __name__ == '__main__':
n = int(input())
for i in range (1,11):
result=n*i
print('%s x %s = %s'%(n,i,result))

Note: The format string expression: print(‘%s x %s = %s’%(n,i,result)). Use tuple (n,i,result) to pass value into the formatted experession.

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2020-2021 By Yang Yue
  • Visitors: | Views:

请我喝杯奶茶吧~