Interactive Python:Mini-project # 3 - "Stopwatch: The Game"

An Introduction to Interactive Programming in Python 

Mini-project development process

  1. Construct a timer with an associated interval of 0.1 seconds whose event handler increments a global integer. (Remember thatcreate_timer takes the interval specified in milliseconds.) This integer will keep track of the time in tenths of seconds. Test your timer by printing this global integer to the console. Use the CodeSkulptor reset button in the blue menu bar to terminate your program and stop the timer and its print statements. Important: Do not use floating point numbers to keep track of tenths of a second! While it's certainly possible to get it working, the imprecision of floating point can make your life miserable. Use an integer instead, i.e., 12 represents 1.2 seconds.
  2. Write the event handler function for the canvas that draws the current time (simply as an integer, you should not worry about formatting it yet) in the middle of the canvas. Remember that you will need to convert the current time into a string using str before drawing it.
  3. Add "
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 当前日期和时间可以使用C#中的DateTime类来获取,然后使用ToString方法将其格式化输出。代码如下: DateTime now = DateTime.Now; string formattedDate = now.ToString("yyyy-MM-dd HH:mm:ss"); Console.WriteLine(formattedDate); 2. 身份证号码中包含的出生年月日可以通过字符串的Substring方法来截取。代码如下: string idCardNumber = "31010119800101001X"; string birthDate = idCardNumber.Substring(6, 8); Console.WriteLine(birthDate); 3. 可以使用Thread.Sleep方法来实现程序的暂停。代码如下: DateTime startTime = DateTime.Now; for (int i = 0; i < 10; i++) { Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); Thread.Sleep(1000); } DateTime endTime = DateTime.Now; TimeSpan duration = endTime - startTime; Console.WriteLine("程序执行时间:{0}秒", duration.TotalSeconds); 4. 循环1亿次进行加减乘除的计算,可以使用Stopwatch类来计时。代码如下: Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < 100000000; i++) { int a = 10; int b = 5; int result = 0; switch (i % 4) { case 0: result = a + b; break; case 1: result = a - b; break; case 2: result = a * b; break; case 3: result = a / b; break; } } stopwatch.Stop(); Console.WriteLine("循环花费时间:{0}毫秒", stopwatch.ElapsedMilliseconds); 5. 可以使用int.TryParse方法来进行字符串到整型数据的转换,并使用try-catch语句来处理异常。代码如下: Console.Write("请输入数字:"); string input = Console.ReadLine(); try { int number; bool success = int.TryParse(input, out number); if (success) { Console.WriteLine("输入的数字为:{0}", number); } else { Console.WriteLine("输入的数据不是数字。"); } } catch (Exception ex) { Console.WriteLine("发生异常:{0}", ex.Message); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值