写一个猜数字脚本,当用户输入的数字和预设数字(随机生成一个小于100的数字)一样时,直接退出,否则让用户一直输入,并且提示用户的数字比预设数字大或者小。
答案
{{{密码回复可见}}}
答案
{{{密码回复可见}}}
0
- #!/usr/bin/env python
- # coding=utf-8
- import random
- import sys
- while True:
- input = int(raw_input('请输入一个小于100的整数:').strip())
- num = random.randint(0,100)
- if input == num :
- sys.exit()
- if input > num :
- print '用户的数字比预设的数字大'
- if input < num :
- print '用户的数字比预设的数字小'
0
本帖最后由 lucas1202 于 2015-8-25 15:11 编辑
#!/bin/bash
m=`echo $(($RANDOM%100))`
echo $m
while :
do
read -p "please input a number:" n
n1=`echo $n|sed 's/[0-9]//g'`
if [ ! -z $n1 ]
then
echo "请输入数字"
elif [ $n -eq $m ]
then
echo "恭喜你,猜对了"
exit 0
elif [ $n -lt $m ]
then
echo "您猜的数字比较小"
continue
else [ $n -gt $m ]
echo "你猜的数字比较大"
continue
fi
done
#!/bin/bash
m=`echo $(($RANDOM%100))`
echo $m
while :
do
read -p "please input a number:" n
n1=`echo $n|sed 's/[0-9]//g'`
if [ ! -z $n1 ]
then
echo "请输入数字"
elif [ $n -eq $m ]
then
echo "恭喜你,猜对了"
exit 0
elif [ $n -lt $m ]
then
echo "您猜的数字比较小"
continue
else [ $n -gt $m ]
echo "你猜的数字比较大"
continue
fi
done
0
while :
do
read -t 10 -p "Please input a number:" n
declare -i aa=`echo $RANDOM | awk -F "" '{print $2 $3}'`
if [ "$n" == "$aa" ];then
exit
elif [ "$n" -gt "$aa" ];then
echo "Bigger than the number system set."
echo $aa
else
echo "Smaller than the number system set."
echo $aa
fi
done
do
read -t 10 -p "Please input a number:" n
declare -i aa=`echo $RANDOM | awk -F "" '{print $2 $3}'`
if [ "$n" == "$aa" ];then
exit
elif [ "$n" -gt "$aa" ];then
echo "Bigger than the number system set."
echo $aa
else
echo "Smaller than the number system set."
echo $aa
fi
done
0
#!/bin//bash
n=$RANDOM
read -p "please guess the num: " num
m=0
while [ $m -eq 0 ]
do
if [ $num -eq $n ];then
echo "you are right"
m=1
elif [ $num -lt $n ];then
read -p "too small, please input again: " num
m=0
else
read -p "too big, please input again: " num
m=0
fi
done
n=$RANDOM
read -p "please guess the num: " num
m=0
while [ $m -eq 0 ]
do
if [ $num -eq $n ];then
echo "you are right"
m=1
elif [ $num -lt $n ];then
read -p "too small, please input again: " num
m=0
else
read -p "too big, please input again: " num
m=0
fi
done
0
本帖最后由 方琪 于 2015-11-28 23:53 编辑
哈哈 鼓捣了半天才鼓捣出来
#!bin/bash
function rand()
{
min=$1
max=$[$2-$1]
retnum=$[RANDOM%max+$1]
echo "$retnum"
}
a=1
b=100
num=$(rand $a $b)
echo $num
while :
do
read -p "please input a number which less than 100:" n
k=$[$n-$num]
#echo $k
case "$k" in
[k==0])
echo "Congratulations! you guess it"
echo $num
exit
;;
*)
if [ $k -gt 0 ]
then
echo "the number you input is too big "
else
echo "the number you input is too small"
fi
esac
done
哈哈 鼓捣了半天才鼓捣出来
#!bin/bash
function rand()
{
min=$1
max=$[$2-$1]
retnum=$[RANDOM%max+$1]
echo "$retnum"
}
a=1
b=100
num=$(rand $a $b)
echo $num
while :
do
read -p "please input a number which less than 100:" n
k=$[$n-$num]
#echo $k
case "$k" in
[k==0])
echo "Congratulations! you guess it"
echo $num
exit
;;
*)
if [ $k -gt 0 ]
then
echo "the number you input is too big "
else
echo "the number you input is too small"
fi
esac
done
0
- #!/bin/bash
- num=$[$RANDOM%100]
- while :
- do
- read -p "请输入一个100内的数字:" n
- n1=`echo -n $n|sed 's/[^0-9]//g'`
- if [[ -z $n1 ]];then
- echo "你输入的不是一个有效的数字"
- continue
- elif [[ $n1 -gt $num ]]; then
- echo "你输入的数字大了"
- continue
- elif [[ $n1 -lt $num ]]; then
- echo "你输入的数字小了"
- continue
- elif [[ $n1 -eq $num ]]; then
- echo "恭喜你,猜对了!"
- exit
- fi
- done
0
你这是自己写了一个随机函数再引用它,系统中自带$RANDOM函数,用于随机生成一个不大于5位数的,可用这个试试
方琪 发表于 2015-11-28 23:16
哈哈 鼓捣了半天才鼓捣出来
#!bin/bash
你这是自己写了一个随机函数再引用它,系统中自带$RANDOM函数,用于随机生成一个不大于5位数的,可用这个试试
0
本帖最后由 hlymlv 于 2016-1-7 21:32 编辑
学习了
#!/bin/bash
m=$(($RANDOM%100+1))
read -p "please enter a number:" n
while : ;do
if [ $n -lt $m ];then
echo "little"
elif [ $n -gt $m ];then
echo "more"
else
echo "bingo"
exit
fi
read -p "please enter a number:" n
done
学习了
#!/bin/bash
m=$(($RANDOM%100+1))
read -p "please enter a number:" n
while : ;do
if [ $n -lt $m ];then
echo "little"
elif [ $n -gt $m ];then
echo "more"
else
echo "bingo"
exit
fi
read -p "please enter a number:" n
done
0
#! /bin/bash
while true
do
Ss=`awk 'BEGIN{srand();print rand()*100}' |awk -F '.' '{print $1}'`
read -p "Pls a number:"
if [[ ${Ss} -gt $1 ]];then
echo "The number is small."
elif [[ ${Ss} -lt $1 ]];then
echo "The number is big."
else
echo "Congratulations, guess"
break
fi
done
while true
do
Ss=`awk 'BEGIN{srand();print rand()*100}' |awk -F '.' '{print $1}'`
read -p "Pls a number:"
if [[ ${Ss} -gt $1 ]];then
echo "The number is small."
elif [[ ${Ss} -lt $1 ]];then
echo "The number is big."
else
echo "Congratulations, guess"
break
fi
done
0
#!/bin/bash
random=$((RANDOM%100))
echo 'random number:' $random
read -p "please enter a number:" n
while :
do
pd=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n" ] && [ -z "$pd" ] #数字全部被替换为空
then
if [ $n -gt $random ]
then
echo 'your number:' $n ' bigger'
elif [ $n -lt $random ]
then
echo 'your number:' $n ' smaller'
elif [ $n -eq $random ]
then
echo 'your number:' $n ' equal'
break
fi
fi
read -p "please try again:" n
done
random=$((RANDOM%100))
echo 'random number:' $random
read -p "please enter a number:" n
while :
do
pd=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n" ] && [ -z "$pd" ] #数字全部被替换为空
then
if [ $n -gt $random ]
then
echo 'your number:' $n ' bigger'
elif [ $n -lt $random ]
then
echo 'your number:' $n ' smaller'
elif [ $n -eq $random ]
then
echo 'your number:' $n ' equal'
break
fi
fi
read -p "please try again:" n
done
0
#!/bin/bash
num=$(($RANDOM%100))
while :
do
read -p "请输入你要猜的数字0-100: " n
if [ -z $n ]
then
echo "你需要输入一个数字"
continue
fi
n1=`echo $n |sed 's/[0-9]//g'`
if [ ! -z $n1 ]
then
echo "只能输入数字"
continue
elif [[ $n -lt $num ]]
then
echo "你输入的数字小了"
continue
elif [[ $n -gt $num ]]
then
echo "你输入的数字大了"
continue
elif [[ $n -eq $num ]]
then
echo "你猜对了"
exit 0
fi
done
num=$(($RANDOM%100))
while :
do
read -p "请输入你要猜的数字0-100: " n
if [ -z $n ]
then
echo "你需要输入一个数字"
continue
fi
n1=`echo $n |sed 's/[0-9]//g'`
if [ ! -z $n1 ]
then
echo "只能输入数字"
continue
elif [[ $n -lt $num ]]
then
echo "你输入的数字小了"
continue
elif [[ $n -gt $num ]]
then
echo "你输入的数字大了"
continue
elif [[ $n -eq $num ]]
then
echo "你猜对了"
exit 0
fi
done
0
- #!/bin/bash
- # 2016-01-28 12:21:23
- # Guess number
- result=$[$RANDOM%100]
- while :
- do
- read -p "input a number:" number
- if [ $number -eq $result ]
- then
- echo "You guess the right number"
- exit
- elif [ $number -gt $result ]
- then
- echo "You input number is bigger than the guess number"
- elif [ $number -lt $result ]
- then
- echo "You input number is smaller than the guess number"
- fi
- done
0
m=`echo $RANDOM`
while :;
do
read -p "Please input a number: " n
s=`echo $n |sed -r 's/[0-9]//g'`
if [ ! -z $s ]
then
echo -n "$n not number"
break
else
if [ $n -gt $m ]
then
echo -n " $n gt number "
elif [ $n -lt $m ]
then
echo -n "$n lt number"
else
echo -n " $n == number"
break
fi
fi
echo
done
while :;
do
read -p "Please input a number: " n
s=`echo $n |sed -r 's/[0-9]//g'`
if [ ! -z $s ]
then
echo -n "$n not number"
break
else
if [ $n -gt $m ]
then
echo -n " $n gt number "
elif [ $n -lt $m ]
then
echo -n "$n lt number"
else
echo -n " $n == number"
break
fi
fi
echo
done
0
- #!/bin/bash
- m=$(($RANDOM%100))
- while :
- do
- read -p "请输入0-100的数字: " n
- if [ $n -lt $m ]
- then
- echo "你输入的数字小了"
- continue
- elif [ $n -gt $m ]
- then
- echo "你输入的数字大了"
- continue
- else
- echo "猜中!"
- fi
- done
- exit
0
#!/bin/bash
num=$[$RANDOM%99+1]
echo $num
while :
do
read -p "请输入一个1到100的数字" x
if [ $x -eq $num ]
then
exit
elif [ $x -gt $num ]
then
echo "数值大于系统预设的数值,请重新输入:"
else
echo "数值小于系统预设的数值,请重新输入:"
fi
done
num=$[$RANDOM%99+1]
echo $num
while :
do
read -p "请输入一个1到100的数字" x
if [ $x -eq $num ]
then
exit
elif [ $x -gt $num ]
then
echo "数值大于系统预设的数值,请重新输入:"
else
echo "数值小于系统预设的数值,请重新输入:"
fi
done
0
- #!/bin/bash
- ##
- ##
- number=56
- read -t30 -p "please input a number: " num
- while [ $num -ne $number ]
- do
- if [ $num -lt $number ];then
- echo "the number less than system number!"
- read -t30 -p " Enter again: " num
- else
- echo "The number greater than system number!"
- read -t30 -p " Entrer again: " num
- fi
- done
- echo "The number you entered is the same as the system"
0
m=`echo $(($RANDOM%100))`
while :
do
read -p "请输入100以内的数:" n
if [$n -eq $m]
then
echo "输入正确"
exit 0
elif [ $n -lt $m ]
then
echo "你猜的数字比较小"
continue
else [ $n -gt $m ]
echo "你猜的数字比较大"
continue
fi
done
while :
do
read -p "请输入100以内的数:" n
if [$n -eq $m]
then
echo "输入正确"
exit 0
elif [ $n -lt $m ]
then
echo "你猜的数字比较小"
continue
else [ $n -gt $m ]
echo "你猜的数字比较大"
continue
fi
done
0
#!/bin/bash
b=1
a=0
while [ $a -ne $b ]
do
read -p "please input your words: " a
while [ -z $a ]
do
read -p "please retry it:" a
done
b=`shuf -i 1-100 -n1`
if [ $a -gt $b ]
then
echo "$a > $b"
else
echo "$a < $b"
fi
done
b=1
a=0
while [ $a -ne $b ]
do
read -p "please input your words: " a
while [ -z $a ]
do
read -p "please retry it:" a
done
b=`shuf -i 1-100 -n1`
if [ $a -gt $b ]
then
echo "$a > $b"
else
echo "$a < $b"
fi
done
0
- #!/usr/bin/env python
- a=`echo $RANDOM | cut -c 1,2`
- #echo $a
- while :; do
- read -p "please input a number: " x
- if [ $x == $a ];then
- echo "you are right"
- break
- elif [ $x -gt $a ];then
- echo "The number what you input is bigger "
- continue
- elif [ $x -lt $a ];then
- echo "The number what you input is smaller"
- continue
- fi
- done
0
- #!/bin/bash
- m=`echo $(($RANDOM%100))`
- echo $m
- while :
- do
- read -p "Please input a number:" n
- n1=`echo $n|sed 's/[0-9]//g'`
- if [ -n "$n1" ]
- then
- echo "The input is not a number."
- elif [ $n -lt $m ]
- then
- echo "The input is small"
- continue
- elif [ $n -gt $m ]
- then
- echo "The number is large"
- continue
- else [ $n -eq $m ]
- echo "The input is right"
- exit 0
- fi
- done
0
本帖最后由 标哥 于 2016-8-30 16:49 编辑
- #! /bin/bash
- # cai shu zi games.
- truenumber=$[ RANDOM % 100 ]
- while :
- do
- read -p "please input a number: " number
- if [ $number != $truenumber ]
- then
- echo "false,please try again."
- if [ $number -gt $truenumber ]
- then
- echo "bigger"
- continue
- else
- echo "smaller"
- continue
- fi
- else
- echo " ok ,you are right."
- exit 0
- fi
- done
编辑回复