Python exercises two. Use the Randint function in random to randomly generate a preset integer between 1~100 to let the user keyboard enter the number of guesses.

Source: Internet
Author: User
Tags shuffle

Python Exercises

Tags: python python practice python knowledge points

two. Use the Randint function in random to randomly generate a preset integer between 1~100 to let the user keyboard input the number of guesses, if it is larger than the preset number, the screen display "too big, please re-enter" if it is less than the preset number, the screen display "too small, please re-enter" so loop, Until guess, show "Congratulations, guess!" Guess n times "N is the number of user guesses.

Answer:

ImportRandomdefGuess_number (): True_num=Random.randint (1, -) User_num= int(input("Please enter an integer:")) Count= 1     whileTrue_num!=User_num:ifTrue_num>User_num:Print("Too small, please re-enter!" ")elifTrue_num<User_num:Print("Too big, please re-enter!" ") Count+= 1User_num= int(input("Please enter an integer:"))Print("Congratulations, you guessed it!" You guessed it altogether.%dTimes " %Count) Guess_number ()
Knowledge points the random module in 1.Python 1.1 Random Module Introduction

The random function in the Python standard library can generate randomly floating-point numbers, integers, strings, and even help you randomly select an element in a list sequence, disrupting a set of data, and so on.

1.2 Random Module method Description
    • Random.random (): The function generates a random floating-point number, the range is between 0.0~1.0
In [2]: import randomIn [3]: random.random()Out[3]: 0.6935051182120364
    • Random.uniform (A, B): A function randomly generates a floating-point number in the range [A, a]
In [26]: random.uniform(0, 100)Out[26]: 26.977426505683276
    • Random.randint (A, B): Randomly generates an integer in a range [A, b] (int type)
In [28]: random.randint(1,2)Out[28]: 2In [29]: random.randint(1,2)Out[29]: 1
    • Random.choice (): You can select a random element from any sequence, such as the list, to return it, which can be used for strings, lists, tuples, and so on.

When the parameter is a list:

In [31]: random.choice([1,2,3])Out[31]: 3In [32]: random.choice([1,2,3])Out[32]: 1

When the argument is a string:

In [38]: random.choice("i am a bad boy")Out[38]: ‘y‘In [39]: random.choice("i am a bad boy")Out[39]: ‘b‘

When the parameter is Ganso:

In [41]: random.choice((1,3,7,4))Out[41]: 1In [42]: random.choice((1,3,7,4))Out[42]: 7
    • Random.shuffle: If you want to shuffle an element in a sequence (excluding Ganso and strings), you can use this function method
In [49]: list = [1,2,3,4]In [50]: random.shuffle(list)In [51]: listOut[51]: [4, 2, 1, 3]
    • Random.sample (A, B): A fragment of the specified length B is intercepted randomly and independently from sequence a.
In [58]: b = (9,9,9,1,2)In [59]: random.sample(b, 2)Out[59]: [9, 1]In [60]: random.sample(b, 2)Out[60]: [1, 9]In [61]: random.sample(b, 2)Out[61]: [1, 9]In [62]: random.sample(b, 2)Out[62]: [1, 9]In [63]: random.sample(b, 2)Out[63]: [2, 9]In [64]: list = [1,2,3,5,7,94,2]In [65]: random.sample(list, 3)Out[65]: [1, 5, 7]In [66]: random.sample(list, 3)Out[66]: [2, 2, 5]In [67]: random.sample("i am a bad boy", 3)Out[67]: [‘ ‘, ‘a‘, ‘b‘]In [68]: random.sample("i am a bad boy", 3)Out[68]: [‘a‘, ‘y‘, ‘b‘]
2. The difference between the randint of the random module in Python and the randint of the Numpy.random module
    • Random.randint (A, b) # randomly returns an integer value in the range [A, b] of the closed interval

    • Numpy.random.randint (A, b) # randomly returns an integer value within the range [A, b] of the Open interval

In [69]: random.randint(0,1)  Out[69]: 0  In [70]: random.randint(0,1)  Out[70]: 1  In [71]: numpy.random.randint(0,1)  Out[71]: 0  In [72]: np.random.randint(0,1)  Out[72]: 0

Python exercises two. Use the Randint function in random to randomly generate a preset integer between 1~100 to let the user keyboard enter the number of guesses.

Related Article

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.