python 程序求助! 有两个!求高手用题目的语言编两个程序!

[复制链接]
查看11 | 回复3 | 2010-10-29 20:41:45 | 显示全部楼层 |阅读模式
Question 2 (Boolean-returning function)
Write a function called isPrime(n) whose task is to determine if an integer n is prime (evenly divisible by only 1 and itself). Pass the integer to be tested to the function as an argument.
To test if a given number n is prime or not, use a loop to divide that number by all numbers from 2 to n-1. As soon as the remainder of the division is zero, the function should return False (n is not prime). However, if after processing all of the elements, and none of the divisions gives a zero remainder, the loop should terminate and you should return True (n is a prime number).
Recall, 2, 3, 5, 7 are all prime numbers because they are evenly divisible by only 1 and themselves. 4, is not prime as it is divisible by 1, 2 and itself.
When you have a function that can properly determine whether an integer is prime or not, write a main method that reads an argument from the user, call the method isPrime(), and depending on the return value of the method, prints an appropriate message whether that number is prime or not.
Below is some sample output:
python question2.py
Please enter an integer 17
17 is a prime number
python question2.py
Please enter an integer 20
21 is not a prime number
Question 3: (Sentinel-controlled while loop and random numbers)
Write a simple addition game (question3.py). The game will present the user with simple addition questions the operands of which should be randomly (python's random) generated integers between 0 and 10. After each question the user is notified whether their answer is correct or incorrect. The game presents the game presents the first question automatically but after each question it asks the user if they want to continue. If the user enters 'yes' the game continues, with any other input it quits. Before the game quits it displays to the user the number of questions correctly answered in both integer and percentage formats. See the output below for clarification.
Once you have the addition programming working, introduce a difficulty level - prompt the user as the game begins to enter level 1, 2 or 3. If level 1 is chosen, the random numbers are between 0 and 10. If level 2 is chosen the numbers are between 0 and 20, and if level 3 is chosen the numbers are between 0 and 50.
python question3.py
1: 0 + 4
answer:4
Correct!
Continue?:yes
2: 5 + 8
answer:13
Correct!
Continue?:yes
3: 5 + 9
answer:14
Correct!
Continue?:yes
4: 3 + 2
answer:6
Incorrect.
Continue?:yes
5: 9 + 9
answer:18
Correct!
Continue?:yes
6: 5 + 9
answer:14
Correct!
Continue?:no
Number of Correct Answers: 5 Percentage: 83.3333333333 %

回复

使用道具 举报

千问 | 2010-10-29 20:41:45 | 显示全部楼层
完全按照要求来的:====== q1.py ======#coding: utf-8def isPrime(n):
for x in range(2, n):
if n % x == 0:
return False
return Truedef main():
num = raw_input("Please enter an integer ").strip()
try:
num = int(num)
except:
print "Invalid input, exit."
retu
回复

使用道具 举报

千问 | 2010-10-29 20:41:45 | 显示全部楼层
又来了。。。 我比较好奇你弄这些玩意干嘛。。
回复

使用道具 举报

千问 | 2010-10-29 20:41:45 | 显示全部楼层
老师的作业他是在国外的留学生建议去http://stackoverflow.com/问老外吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行