Guess achieve applet

Guess achieve applet

A size comparison of two numbers

Implementation code:

# 设计思路
# 1.输入第一个数字num1
# 2.输入第二个数字num2
# 3.判断两个数字的大小
# 4.输出判断结果

#设计代码
num1=float(input("请输入第一个数字:"))
num2=float(input("请输入第二个数字:"))

if num1>num2:
    print(str(num1) + '>' +str(num2))
elif num1<num2:
    print(str(num1) + '<' +str(num2))
else:
    print(str(num1) + '=' +str(num2))

Achieve results:

II. Numberguess (Age) small game

2.1 Guess achieve a small program with a conditional statement

Implementation code

1. a fixed number

Implementation code:

# 设计思路  程序设定一个数字,用户输入一个数字,判断是否猜对。
temp=input("猜猜我心中的数字:")
guess=int(temp)
if guess==8:
    print("猜对!")
else:
    print("猜错了!")
print("游戏结束!")

Achieve results:

2.2 Random Number

Implementation code:

设计思路  上一个程序中,用户猜错要重新运行程序,嵌套while循环让用户可以一直猜,知道猜对。另外,系统设定的数字不能是静态的,要改为随机生成
1.随机选择一个随机年龄并赋值,在范围(1,100)内
2.猜测一个年龄并赋值
3.比较猜测的年龄与所选的年龄之间的大小
4.猜测错误,循环继续判断
5.猜测正确,退出猜测

设计代码
import random
temp = input("猜猜我的年龄:")
guess=int(temp)
secret=random.randint(1,100)
while guess!=secret:
    if (guess<secret):
        print("猜小了!")
    else:
        print("猜大了!")
    temp=input("猜猜我的年龄:")
    guess = int(temp)
print("猜对!游戏结束!")

Achieve results:

2.3 limit the number of guesses

Now, users can only have three chances to Guess. We can modify the conditions of the cycle, when the user did not run out chance and guess, it has been the implementation of this loop.

Implementation code:

# 现在,用户只能有三次机会来猜数字。我们可以修改循环的条件,当用户没猜中并且机会还没用完,就一直执行这个循环体
import random
temp = input("猜猜我心中的数字:")
guess = int(temp)
secret = random.randint(1,10)
i = 2
while (guess!=secret)and(i):
    if (guess < secret):
        print("猜小了!")
        print("剩余机会次数:",i)
    else:
        print("猜大了!")
        print("剩余机会次数:", i)

    temp = input("猜猜我心中的数字:")
    guess = int(temp)
    i = i - 1
else:
    if(i>0):
        print("猜对!游戏结束!")
    else:
        print("你的机会用完!")

Achieve results:


Author: Luo Wenxiang
Source: Cheung SHAO
Original: https://www.cnblogs.com/LWX-YEER/p/11183260.html
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/LWX-YEER/p/11183260.html