Exercise 1: Basic Python Syntax Elements (Week 1) (mooc)

I recently learned python and got used to c syntax. Learning python is still a headache, especially after indentation, it is not far from crazy.


Hello World I

 

describe

This is the first instance of learning each programming language.

Output Hello World, pay attention to capitalization.

 


enter

without


output

Hello World

Code:

print("Hello World")



Hello World II

 

describe

Vertical output "Hello World", the entire code does not exceed 2 lines.

 


enter

without


output

H

e

l

l

O

 

W

O

r

l

d


The following ways of writing are provided:

Code one:

print("H\ne\nl\nl\no\n \nW\no\nr\nl\nd\n")
Code two:

s="Hello World"
for i in s:
    print(i)
Code three:

 
  
s="Hello World"
for i in range(len(s)):
 print(s[i])

Multiple powers of N 

describe

Write a program that calculates the result from the 0th power to the 5th power of the input number N, and outputs the 6 results in sequence, separating the output results with spaces. where: N is an integer or floating point number.

The print() function can output multiple information at the same time. The following methods can be used to split multiple output results with spaces:

print(3.14, 1024, 2048)

This platform can obtain test case input through the input() function. Please note, do not add prompt information parameters in input() , use the following methods to obtain test case input and output it:

a = input()
print(a)

 


enter

Example 1:2

 


output

Example 1: 1 2 4 8 16 32

If you use pow, it will return floating point. There are integers, floating point, integer after integer calculation, floating point calculation is floating point, so there is no need for pow
code :

a =eval(input())
print(a**0,a**1,a**2,a**3,a**4,a**5)


Temperature Conversion II

 

describe

There are two different systems for describing temperature: Celsius (Celsius) and Fahrenheit (Fabrenheit).

Write a program to convert user input from degrees Fahrenheit to degrees Celsius, or to convert input from degrees Celsius to degrees Fahrenheit.

The conversion algorithm is as follows: (C for Celsius, F for Fahrenheit)

         C = ( F - 32 ) / 1.8

         F = C * 1.8 + 32

Requirements are as follows:

(1) The input and output degrees Celsius start with a capital letter C, and the temperature can be an integer or a decimal, such as: C12.34 refers to 12.34 degrees Celsius;

(2) The Fahrenheit input and output start with a capital letter F, and the temperature can be an integer or a decimal, such as: F87.65 refers to 87.65 degrees Celsius;

(3) The problem of abnormal input is not considered, and the output retains two decimal places;

(4) When using input() to obtain test case input, do not add a prompt string.

 


enter

Example 1: C12.34

Example 2: F87.65

 


output

Example 1: F54.21

Example 2: C30.92

Code:
a=input()
if a[0] in ['C']:
  F=eval(a[1:])*1.8+32
  print("F%.2f"%F)
elif a[0] in ["F"]:
  C=(eval(a[1:])-32)/1.8
  print("C%.2f"%C)#or this format control print("C{:.2f}".format(C))


Currency Conversion I 

describe

The RMB and the US dollar are one of the two currencies commonly used in the world. Write a program to convert the currency value between currencies, among which:

The exchange rate between RMB and USD is fixed at: 1 USD = 6.78 RMB.

The program can accept RMB or USD input and convert to USD or RMB output. Renminbi is expressed in RMB, and USD is expressed in USD. There is no space between the symbol and the value.

Notice:

(1) This is an OJ question, please use input() to get input 

 


enter

 

Example 1: RMB123

Example 2: USD20


output

 

Example 1: USD18.14

Example 2: RMB135.60

 Code:

money=input()
if money[0]=='R':
  ans=eval(money[3:])/6.78
  print("USD%.2f"%ans)#或者这样写print("USD{:.2f}".format(ans))
else:
  ans=eval(money[3:])*6.78
  print("RMB%.2f"%ans)#或者这样控制输出print("RMB{:.2f}".format(ans))




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685289&siteId=291194637