Python Exercise 2

Source: Internet
Author: User

1. Use a recursive function to print the five characters you have entered in reverse order


#-*-Coding:utf8-*-
# AUTHOR:WXQ
#python 2.7
#题目: The 5 characters entered are printed in reverse order using the recursive function invocation method.
#定义一个字符串, waiting for user input
string = input ("Please enter a string:")
#定义一个变量
def f (x):
if x = =-1: #当变量的长度是-1, return
Return '
else: #否则返回字符串的位置向后移1位 until the length of the variable is-1
return string[x] + f (x-1)
Print (f (Len (String)-1))

3. Determine if a number is prime

# User Input number
num = Int (input ("Please enter a number:"))

# prime number greater than 1
If num > 1:
# View Factors
For I in Range (2,num):
if (num% i) = = 0:
Print (num, "not prime number")
Print (I, "multiply in", num//i, "yes", num)
Break
Else
Print (num, "is prime")

# If the number entered is less than or equal to 1, not prime
Else
Print (num, "not prime number")


4. Define the factorial function, pass an int parameter, return all its digits multiplied by the number of ringgit, such as 1234 return (1*2*3*4)

1234//10//10//10

def factorial (i):
result = 1
while (I/10 > 0):
Result *= I% 10
i = Int (I/10)
# result *= I
return result


Print (factorial (1234))

#########

Indentationerror:unindent does not match any outer indentation level appears in Python

47089473


Python newspaper "indentationerror:unexpected indent" solution.

10379157

Python Exercise 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.