0 Fundamentals python-2.15 Back to our games add for and list

Source: Internet
Author: User

Let's start by pasting the previous code and reviewing the functionality

Import Randomsecret=random.randint (1,100) #生成随机数 #print (secret) time=6# guess number of times guess=0# input number minnum=0# minimum random number maxnum=100 #最大随机数print ("---------Welcome to the place where you guessed the numbers, start---------") while Guess!=secret and time>=0: #条件    guess=int ("* Number Interval 0-100, enter the number you guessed: ")"    print ("You enter the number is:", guess)    if Guess==secret:        print ("guessed right, really bad")    else:        # When not equal, you also need to print out the corresponding interval, making it easier for users to use        if Guess<secret:            minnum=guess            print ("Your guess is less than the correct answer")                        print (" The current number range is: ", Minnum,"-", Maxnum)        else:            maxnum=guess            print (" Your guess is greater than the correct answer ")            print (" Number range is: ", minnum , "-", Maxnum)        print ("Too bad, you guessed wrong, you have", Time, "chance")    Time-=1print ("Game Over")

In the above can only one user playing, not able to support multi-user, and do not record the user's achievements, we improve the above code, so that he supports multi-user, as well as record the user's achievements

So, we introduce a list as a carrier for recording user results, and then introduce a for statement as a looping print score

Below we enter the code:

Import randomexittag=0userlist={}while exittag!=1:username=input ("Please enter your name:") if username== ' exit ': exittag=1    Break Else:secret=random.randint (1,100) #生成随机数 #print (secret) time=6# guess number of times guess=0# input number mi nnum=0# minimum random number maxnum=100# max random number print ("---------Welcome to the place where you guess the number, start---------") while Guess!=secret and time& Gt;=0: #条件 guess=int (Input ("* Number range 0-100, please enter your guess number:")) Print ("You enter the number is:", guess) if guess==secre T:userlist[username]=7-time print ("guessed right, really awesome") print ("History:") F                or item in Userlist:print (item+ "only used" +STR (Userlist[item]) + "guess") Else:                        #当不等于的时候, you also need to print out the appropriate intervals to make it easier for users to use if Guess<secret:if minnum<guess: Minnum=guess print ("Hint: your guess is less than the correct answer") Print ("Hint: the correct number range is: ", MinnuM, "-", Maxnum) else:if maxnum>guess:maxnum=guess Print ("Hint: your guess is greater than the correct answer") Print ("Hint: the correct number range is:", Minnum, "-", Maxnum) print ("Too sorry you guessed wrong, you still Have ", Time," chance ") Time-=1print (" Game Over ")


This is the section here, thank you.

------------------------------------------------------------------

Click to jump 0 basic python-Catalogue









Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

0 Fundamentals python-2.15 Back to our games add for and list

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.