day02 homework
1. Guess the number, set an ideal number such as: 66, let the user input the number, if larger than 66, the result of the guess is larger; if smaller than 66, the result of the guess is smaller; only equal to 66, the result of the guess is correct, and then exit the loop.
value= 66 while True: guessvalue = int(input("please enter a number:")) if guessvalue > value: print("Big") elif guessvalue < value: print ("Small") else: print("Guess correctly") break
2. On the basis of the previous question, set up: give users three guesses, if guessed correctly within three times, then show the correct guess, exit the cycle, if not guessed correctly within three times, then automatically exit the cycle, and show the "big fool".
guessnumber = 3 value = 66 while guessnumber: guessvalue = int(input("please enter a number:")) if guessvalue > value: print("Big") guessnumber-=1 elif guessvalue < value: print ("Small") guessnumber -= 1 else: print("Guess correctly") break if guessnumber==0: print ("Idot")
3. Two methods are used to realize the output of 123 4 5 6 8 9 10.
count = 0 while count !=10: count +=1 if count==7: continue else: print(count)
for i in range(1,11): if i ==7: continue else: print(i)
4. Find the sum of all numbers of 1-100
sum = 0 for i in range(1,101): sum +=i print(sum) ##############or################# sum = 0 count = 0 while count < 100: count += 1 sum +=count print (sum)
5. Output of all odd numbers in 1-100
for i in range(1,101,2): print(i) ##############or################# num = 1 while num < 100-1: print(num+2) num +=2
6. All Even Numbers in Output 1-100
for i in range(2,101,2): print(i) ##############or################# num = 0 while num < 100: print(num+2) num +=2
7. Find 1-2+3-4+5... The sum of all numbers of 99
tag=1 sum=0 for i in range(1,100): #print(i) sum += i * tag tag *= -1 print(sum) ##############or################# sum = 0 num = 1 flag= 1 while num < 100: sum +=num*flag num +=1 flag=flag*-1 print (sum)
8. User login (three error opportunities) and display the number of remaining errors per error (prompt: using string formatting)
allchance = 3 num = 0 user = "frantic" pwd = "frantic" while num < 3: num += 1 leftchance = allchance - num if user == input("Please enter a user name:") and pwd == input("Please input a password:"): print("Successful login") break else: print("Logon failed, remaining%d Secondary Opportunities"%leftchance)
9. Brief description of ASCII, Unicode, utf-8 coding
- ASCII English, each 8 digits is a unit, a total of 2 ** 8 components, can include all the English letters, numbers and symbols, and can only be in English.
- unicode Universal Code can represent all words, each 32 digits is a unit, a total of 2 ** 32 components. Compared with ASCII, it has no limitations and can express all words and symbols. The disadvantage is that 32 digits are one unit, which wastes resources and is used for memory computing and processing, and it is more regular.
- utf-8 refines the Universal Code and omits the unused digits to save space, but they are all 8-fold digits, mostly used for data transmission.
10. The relationship between the predicate and the byte?
One byte equals eight bits
11. Guess Age Game Requirements: Allow users to try at most three times, and if they do not guess correctly three times, they will quit directly. If they guess correctly, print congratulations and quit.
age = 24 num = 0 while num < 3: num += 1 if age == int(input("Please enter a guessed age:")): print("Congratulations on guessing right") break
12. Guess Age Game Upgrade Requirements: Allow users to try up to three times. If they have not guessed correctly, ask them if they want to continue playing. If they answer Y, they continue to guess three times. In this way, they can go back and forth. If they answer N, they will quit the program. How to guess correctly, they will quit directly.
age = 24 num = 0 while num < 3: num += 1 if age == int(input("Please enter a guessed age:")): print("Congratulations on guessing right") break else: print("Misguessed") if num ==3: value = input("Do you want to continue guessing? input Y Continue, enter N Sign out:") if value == "Y": num = 0 elif value == "N": print("Sign out!") else: print("Input error!")
13. Judge True,False of the following logical statements
- 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 True
- not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 False
14. Find the values of the following logical statements.
8 or 3 and 4 or 2 and 0 or 9 and 7 8
0 or 2 and 3 and 4 or 6 and 0 or 3 4
15. What are the following results?
- 6 or 2 > 1 6
- 3 or 2 > 1 3
- 0 or 5 < 4 false
- 5 < 4 or 3 3
- 2 > 1 or 6 true
- 3 and 2 > 1 true
- 0 and 3 > 1 0
- 2 > 1 and 3 3
- 3 > 1 and 0 0
- 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2 2