-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges_45_51_whileloop.py
103 lines (93 loc) · 3.89 KB
/
challenges_45_51_whileloop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#045
# Set the total to 0 to start with.
# While the total is 50 or less, ask the user to input a number.
# Add that number to the total and print the message “The total is... [total]”.
# Stop the loop when the total is over 50.
total=0
while total<=50:
number=int(input("Enter a number: "))
total=total+number
print(f"Total is {total}.")
#046
# Ask the user to enter a number.
# Keep asking until they enter a value over 5 and
# then display the message “The last number you entered was a [number]”
# and stop the program.
number=0
while number<=5:
number=int(input("Enter a number: "))
print(f"The last number you entered was a {number}")
#047
# Ask the user to enter a number and then enter another number.
# Add these two numbers together and then ask if they want to add another number.
# If they enter “y", ask them to enter another number
# and keep adding numbers until they do not answer “y”.
# Once the loop has stopped, display the total.
number1=int(input("Enter a number: "))
number2=int(input("Enter another number: "))
add=number1+number2
print(f"The addition of these two numbers are {add}.")
ask=input("Do you want to add another number [y/n]?: ").lower()
while ask=="y":
number3=int(input("Enter the number: "))
add=add+number3
ask=input("Do you want to add another number [y/n]?: ").lower()
print(f"The total is {add}.")
#048
# Ask for the name of somebody the user wants to invite to a party.
# After this, display the message “[name] has now been invited” and add 1 to the count.
# Then ask if they want to invite somebody else.
# Keep repeating this until they no longer want to invite anyone else to the party and
# then display how many people they have coming to the party.
count=0
invite="y"
while invite=="y":
name=input("What is the name of the person you want to invite to a party? ").lower()
print(f"{name} has now been invited.")
count+=1
invite=input("Do you want to invite someone else [y/n]? ").lower()
print(f"{count} people are coming to the party.")
#049
# Create a variable called compnum and set the value to 50.
# Ask the user to enter a number.
# While their guess is not the same as the compnum value,
# tell them if their guess is too low or too high and ask them to have another guess.
# If they enter the same value as compnum,
# display the message “Well done, you took [count] attempts”.
compnum=50
count=1
number=int(input("Enter a number: "))
while number!=compnum:
count+=1
if number>compnum:
print("Your guess is too high.")
elif number<compnum:
print("Your guess is too low.")
number=int(input("Enter a number: "))
print(f"Well done, you took {count} attempts.")
#050
# Ask the user to enter a number between 10 and 20.
# If they enter a value under 10, display the message “Too low” and
# ask them to try again. If they enter a value above 20, display the message “Too high”
# and ask them to try again.
# Keep repeating this until they enter a value that is between 10 and 20
# and then display the message “Thank you”.
num=int(input("Enter a number between 10 and 20: "))
while num<10 or num>20:
if num<10:
print("Too low.")
elif num>20:
print("Too high.")
num=int(input("Guess again: "))
print("Thank you.")
#051
# Using the song “10 green bottles”,
# display the lines “There are [num] green bottles hanging on the wall,
# [num] green bottles hanging on the wall, and if 1 green bottle should accidentally fall”.
# Then ask the question “how many green bottles will be hanging on the wall?”
# If the user answers correctly,
# display the message “There will be [num] green bottles hanging on the wall”.
# If they answer incorrectly,
# display the message “No, try again” until they get it right.
#When the number of green bottles gets down to 0, display the message
#“There are no more green bottles hanging on the wall”.