Table of contents
Python operation mathematical expression
This question requires to read in 2 integers A and B, and then output the value of C according to the following formula.
Please fully consider the possible situations of A and B input.
Input format:
The input gives 2 integers A and B in one line.
Output format:
For each set of inputs, output the value of C in one line, retaining two decimal places.
If B enters 0, the input prompts "The denominator cannot be 0."
If the input is wrong, the output "Input is wrong!"
Input example:
A set of inputs is given here. For example:
18 -299
Output example:
The corresponding output is given here. For example:
-876.72
answer
- Use the split() function to split the data in the same line, the default is to split by spaces; use the map() function to assign the split string to the corresponding variables x, y;
- Determine whether the x and y variables conform to the digital law, that is, use the for loop for x and y respectively, and observe the character of each digit;
- Finally, convert x and y into integer types, judge whether the denominator is zero, and output it directly if it is zero; if it is not zero, bring it into the expression to calculate the output result.
code show as below
x, y = map(str, input().split())
for i in range(len(x)):
if x[i] == '-':
if i != 0 or len(x) == 1:
print("输入有误!")
exit(0)
continue
elif '0' <= x[i] <= '9':
continue
else:
print("输入有误!")
exit(0)
for j in range(len(y)):
if y[j] == '-':
if j != 0 or len(y) == 1:
print("输入有误!")
exit(0)
continue
if '0' <= y[j] <= '9':
continue
else:
print("输入有误!")
exit(0)
x = int(x)
y = int(y)
if(y==0):
print("分母不能为0")
else:
c = (2 ** x + 7 - 3 * 4) / y
print("%.2f" % c)
Extract the student number abcd(19) from the string
topic
There are one or two numeric characters in a string to represent the student's student number, now you need to extract the student number, normally, the student number is between a pair of ().
But there are some irregular input, such as abcd(19dk), the student number should be 19. (The last is a number, otherwise it is illegal, return not find. Only one 0 is an illegal student number, output not find.
Input format:
abcd(19)
Output format:
19
Input sample:
ydsf(00)
abc(08)
Output example:
The corresponding output is given here. For example:
not find
8
the code
x = str(input())
left = 0
right = 0
for i in x:
if (i == '('):
left += 1
if (i == ')'):
right += 1
if ((left != 1) | (right != 1)):
print("not find")
exit(0)
x = x.split('(')
x = x[1]
x = x.split(')')
x = x[0]
x = x[0:2]
m = int(0)
if (x == ''):
print("not find")
exit(0)
if (len(x) == 1):
if (x[0] == '0'):
print("not find")
exit(0)
if ((x[0] > '0') & (x[0] <= '9')):
m = int(x[0])
print(m)
exit(0)
if (x == "00"):
print("not find")
exit(0)
if ((x[0] >= '0') & (x[0] <= '9')):
if ((x[1] < '0') | (x[1] > '9')):
m = int(x[0])
print(m)
exit(0)
if ((x[0] >= '0') & (x[0] <= '9')):
if ((x[1] >= '0') & (x[1] <= '9')):
m = int(x[0]) * 10 + int(x[1])
print(m)
exit(0)
print("not find")
Capitalize the first letter of a sentence
topic
Enter a line of sentences and capitalize the first letter of each sentence. There are spaces between every word and every sentence starts with ? or.or! end.
Input format:
enter a string
Output format:
capitalize the first letter of the sentence and output the string
Input sample:
you are a sight sore eyes! you look well.
Sample output:
You are a sight sore eyes! You look well.
the code
x = str(input())
flag = 1
m = ''
for i in x:
if ((i >= 'A') & (i <= 'Z')):
flag = 0
if (flag == 1):
if ((i >= 'a') & (i <= 'z')):
i = i.upper()
flag = 0
if (i == '.'):
flag = 1
if (i == '!'):
flag = 1
if (i == '?'):
flag = 1
m += i
m += ' '
print(m)