吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3364|回复: 12
收起左侧

[Python 转载] Python If-Else 新手挑战

[复制链接]
hkivanleeon68 发表于 2017-10-10 13:33
本帖最后由 hkivanleeon68 于 2017-10-10 13:53 编辑

4.JPG
今天带来"HackerRank" 的 Python 挑战 关卡 2, If-Else. (附带原文章翻译)

帖子末端会提供 关卡2的传送门,有需要的同学也可以注册挑战一下。

Task (题目)
Given an integer, , perform the following conditional actions:
一个特定的整数 n执行以下的条件行动:

If  is odd, print Weird
如果 n 是奇数,打印 Weird (奇怪)


If  is even and in the inclusive range of  to , print Not Weird
如果 n 是偶数而且在3-5的范围,打印 Not Weird (不奇怪)


If  is even and in the inclusive range of  to , print Weird
如果 n 是偶数而且在6-20的范围内,打印 Weird


If  is even and greater than , print Not Weird
如果 n 是偶数而且大于 20,打印 Not Weird



Input Format输入格式

A single line containing a positive integer, n.

单行但包含一个正整数,n

Constraints
条件

2.JPG

n 大于或等于1,同时,n 小於或等于100

Output Format
输出格式
Print Weird if the number is weird; otherwise, print Not Weird.
打印 Weird 如果数字是属于 奇怪的,否则,打印 Not Weird

Sample Input 0

0号例子输入
[Python] 纯文本查看 复制代码
n = 3

Sample Output 0
0号例子输出
[Python] 纯文本查看 复制代码
Weird

Sample Input 1
1号例子输入
[Python] 纯文本查看 复制代码
n = 24

Sample Output 1
1号例子输出
[Python] 纯文本查看 复制代码
Not Weird


Explanation
解释
Sample Case 0: n = 3, n is odd and odd numbers are weird, so we print Weird.
0号例子: n = 3, n 是奇数而且奇数是属于 奇怪的,所以我们打印 Weird
Sample Case 1:  n =  24, n > 20 and n is even, so it isn't weird. Thus, we print Not Weird.
1号例子: n = 24, n 大于 20 而且 n 是偶数,所以它不属于 奇怪的。如此,我们打印 Not Weird


理解完题目就准备开始咯!!!~
捕获.JPG



关卡2 传送门:https://www.hackerrank.com/challenges/py-if-else/problem

附上小弟满分答案一枚。。。
备注:编码中的符号例如(%2,==,=<...)是额外的知识噢,网上有很多资料,吾爱也有很多Python的参考书籍。

愿各位加油,一起努力学习。

捕获1.JPG




1.JPG



发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ryuukyou 发表于 2017-10-11 11:14
if __name__ == '__main__':
    n = int(input())
    if n % 2 != 0:
            print("Weird")
    elif n % 2 == 0 and (3 < n < 5):
            print("Not Weird")
    elif n % 2 == 0 and (6 < n <= 20):
            print("Weird")
    else:
            print("Not Weird")
   
跟楼主的不一样
ccraker 发表于 2017-10-25 17:24
ryuukyou 发表于 2017-10-11 11:14
if __name__ == '__main__':
    n = int(input())
    if n % 2 != 0:

elif n % 2 == 0 and (3 < n < 5):
判断条件多余了,能到这说明n%2已经等于0了。
咸鱼人生 发表于 2017-10-10 13:45
冥界3大法王 发表于 2017-10-10 13:53

费话!代码是用来敲的,直接看只能是肉眼凡胎。
凡是全能看懂了,世上的企业家不就到处都是了嘛。
netCheney 发表于 2017-10-10 13:59
准备尝试一下,如果能够得到正解,我会贴出答案,如果我没贴出答案,请在明年的今天给我点上一支烟
~越飞~ 发表于 2017-10-10 14:04
菜鸟来看过,感觉很厉害的样子
撸管小东 发表于 2017-10-10 14:14
菜鸟来看过,感觉很厉害的样子
wcwujiayu 发表于 2017-10-10 14:16
我昨天也上去看了下,但是英语烂死了。操作学习就不方便了
咸鱼人生 发表于 2017-10-10 18:03
冥界3大法王 发表于 2017-10-10 13:53
费话!代码是用来敲的,直接看只能是肉眼凡胎。
凡是全能看懂了,世上的企业家不就到处都是 ...

好的大佬。。。
 楼主| hkivanleeon68 发表于 2017-10-11 07:27
wcwujiayu 发表于 2017-10-10 14:16
我昨天也上去看了下,但是英语烂死了。操作学习就不方便了

所以我才贴上原文翻译...
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-7 07:48

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表