初次使用Python写代码。
print("Welcome!") g=input("Guess the number: ") guess=int(g) if guess==5: print("You win!") else: if guess>5: print("Too high") else: print("Too low") print("Game over!")
加入循环:
print("Welcome!") guess=0 while guess !=5: g=input("Guess the number: ") guess=int(g) if guess==5: print("You win!") else: if guess>5: print("你猜的偏大!") else: print("Too low") print("game over!")
把只有五的情况改为1-10之间的随机的数:
from random import randint secrer=randint(1,10) print("Welcome!") guess=0 while guess !=secrer: g=input("Guess the number: ") guess=int(g) if guess==secrer: print("You win!") else: if guess>secrer: print("你猜的偏大!") else: print("Too low") print("game over!")
C#代码对比:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace guess { class Program { static void Main(string[] args) { Console.Write("欢迎你来玩猜数字游戏!"); Random rd = new Random(); int number = rd.Next(1,10); int guessnumber; do { Console.Write("请输入你所猜的数字:"); guessnumber = int.Parse(Console.ReadLine()); if(guessnumber>number) { Console.Write("你猜大了!"); } else if(guessnumber<number) { Console.Write("你猜小了!"); } } while(guessnumber!=number); Console.Write("恭喜你猜对了!"); Console.ReadKey(); } } }
想说的是有些事就怕去尝试!两种语言去编写同一个程序,虽然语法上面有些不同,但是不同的语言的编写都是同一个道理,本质是一样的!两种语言也各有个的好处吧1Python写程序更像是在写文本!没有任何文字性的提示!也没有代码的错误提示!但是相对于C#写代码时,感觉Python更简洁一些!
初次用Python写代码还不是很熟练,也请大神们指正!